我是 ruby​​ 的新手,并开始创建我的 *nd 玩具应用程序。
一世:

  • 创建 Controller “问题”
  • 创建模型“问题”
  • 创建的 Controller Action 'new'
  • 添加了“New.html.erb”文件

  • 在 erb 文件中,我使用 form_for helper 和 new Controller 操作,我在其中实例化@question 实例变量。
    当我尝试运行它时,我收到 'undefined method: questions_path for #<ActionView::Base:0x5be5e24>' 错误。
    下面是我的 new.html.erb:
    <%form_for @question do |f| %>
       <%=f.text_field :title %>
    <%end%>
    

    请告知如何解决此问题,并帮助我为该 Controller 操作设置别名。
    我的意思是我想输入 http://mysite/questions/ask ,而不是/questions/create

    最佳答案

    config/routes.rb 中,您需要添加:

    map.resources :questions
    

    修复未定义的方法 questions_path 问题。

    获取 /questions/ask 的一种方法是像这样修改 routes.rb:
    map.ask_question '/questions/ask', :controller => 'questions', :action => 'create'
    

    这会给你 ask_question_path ,你可以在你的代码中引用它。

    关于ruby-on-rails - rails中的未定义方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/330993/

    10-12 14:26
    查看更多