2008-12-01 64 views
4

我是新來的紅寶石,並開始創建我的* ND玩具應用程序。 我:在軌道中的未定義方法

  1. 創建控制器 '問題'
  2. 創建的模型 '問題'
  3. 創建控制器動作 '新'
  4. 加入 'New.html.erb' 文件

在erb文件我使用form_for幫手和和new控制器動作,其中我實例化 @question實例變量。 當我嘗試運行這個時,我得到了'undefined method: questions_path for #<ActionView::Base:0x5be5e24>'錯誤。 下面是我new.html.erb:

<%form_for @question do |f| %> 
    <%=f.text_field :title %> 
<%end%>  

請告知如何解決這個問題,也幫我混淆這個控制器動作。 我的意思是我想鍵入http://mysite/questions/ask,而不是/問題/創建

+0

您是否向route.rb文件添加了任何內容? – 2008-12-01 14:47:07

回答

8

config/routes.rb,你將需要添加:

map.resources :questions 

修復未定義的方法questions_path問題。

一種方式來獲得​​是修改routes.rb像這樣:

map.ask_question '/questions/ask', :controller => 'questions', :action => 'create' 

,這將給你ask_question_path,你可以在你的代碼中引用。

0

看起來你已經完成了所有這些步驟。你應該試試腳手架發電機,這將爲你建立所有這些。

例子:

>ruby script/generate scaffold question question:string answer:string votes:integer 
    exists app/models/ 
    exists app/controllers/ 
    exists app/helpers/ 
    create app/views/questions 
    exists app/views/layouts/ 
    exists test/functional/ 
    exists test/unit/ 
    exists public/stylesheets/ 
    create app/views/questions/index.html.erb 
    create app/views/questions/show.html.erb 
    create app/views/questions/new.html.erb 
    create app/views/questions/edit.html.erb 
    create app/views/layouts/questions.html.erb 
    create public/stylesheets/scaffold.css 
    create app/controllers/questions_controller.rb 
    create test/functional/questions_controller_test.rb 
    create app/helpers/questions_helper.rb 
    route map.resources :questions 
    dependency model 
    exists app/models/ 
    exists test/unit/ 
    exists test/fixtures/ 
    create app/models/question.rb 
    create test/unit/question_test.rb 
    create test/fixtures/questions.yml 
    create db/migrate 
    create db/migrate/20081201150131_create_questions.rb 

因此,大家可以看到,有一個支架,我們得到我們的模型,我們的控制器,我們的觀點,我們的路線,數據庫遷移,將建立一個問題表有兩個字段, RESTful控制器動作立即添加/更新/查看/刪除任何問題數據。哦,最重要的是,一組空白的測試文件已經準備好用於測試了:)。

+0

嗨,感謝您的快速響應。我選擇了手動的方式,因爲我想'搭便車'腳手架的內部步驟,當然,卡住:-) – Valentin 2008-12-01 15:15:15

相關問題