【问题标题】:Form_for path @get_quote vs @get_quotes (undefined method)Form_for 路径 @get_quote 与 @get_quotes (未定义的方法)
【发布时间】:2016-05-03 18:12:55
【问题描述】:

当我使用 get_quote 时,为什么表单在寻找 get_quotes 路径?如何更正错误?

耙子路线

get_quote_index GET    /get_quote(.:format)          get_quote#index

错误:

undefined method `get_quotes_path'

代码:

<%= form_for( @get_quote ,:html => {:class => "form-horizontal"}) do |f| %> 

【问题讨论】:

  • 您能否发布运行rake routes CONTROLLER=get_quote 时获得的全部结果?答案可能就在其中。
  • 我认为,您没有将 get_quote_index 定义为 POST。
  • 什么是get_quote?它是一个未保存的活动记录对象吗?
  • 问题在于命名约定。谢谢!

标签: ruby-on-rails ruby-on-rails-4 routes form-for


【解决方案1】:

错误与@get_quote变量无关,是路径。


解决方案

您应该在路由中使用resources 指令:

#config/routes.rb
resources :get_quotes, path: "get_quote" #-> url.com/get_quote/new

...这将创建一系列您的应用程序将使用的 RESTful 路由。

由于 Rails 的convention over configuration,许多辅助方法,包括form_for,都被设计为使用上面的resources 指令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多