Form_for 路径@get_quote 与@get_quotes(未定义的方法)

Form_for path @get_quote vs @get_quotes (undefined method)

为什么当我使用 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| %> 

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


解决方案

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

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

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

由于 Rails' convention over configuration, many helper methods, including form_for 旨在使用上面的 resources 指令。