2008-12-01 62 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

相关问题