简体   繁体   English

Rails 3.2中form_tag的路由错误

[英]Routing error for form_tag in Rails 3.2

This question probably has been asked many time. 这个问题可能已经问了很多次了。 An custom action mass_onboard_result was declared in routes.rb for engine_config : 一个自定义操作mass_onboard_result被宣布为routes.rb中engine_config

collection do
  put :mass_onboard_result
end

In controller for engine configs, there is definition for mass_onboard_result : 在引擎配置控制器中,有mass_onboard_result定义:

def mass_onboard_result
   code here
end

Here is the view which starts with form_tag : 这是以form_tag开头的视图:

<%= form_tag mass_onboard_result_engine_configs_path do %>
  .......
  <%=submit_tag 'Save' %>
<% end %>

When clicking Save on the form, there is an routing error: 在表单上单击“ Save ”时,出现路由错误:

No route matches [POST] "/onboard_data_upload/engine_configs/mass_onboard_result"

Try running rake routes for more information on available routes.

In output of rake routes , there is: rake routes输出中,有:

mass_onboard_result_engine_configs PUT    /engine_configs/mass_onboard_result(.:format)

What could cause the error. 可能导致错误的原因。 Is it the declaration in routes.rb? 它是routes.rb中的声明吗? Many thanks. 非常感谢。

You have defined your route as a put route, but your form is creating a post request. 您已将路线定义为put路线,但是表单正在创建post请求。

If you add method: :put into the form_tag options, is the problem resolved? 如果将method: :put添加到form_tag选项中,问题是否得到解决?

The error is written here: 错误写在这里:

No route matches [POST]...

A typical problem for Rails devs (especially ones starting out), is to overlook the importance of the HTTP verb in the routes structure. 对于Rails开发人员(尤其是刚开始的开发人员)来说,一个典型的问题是忽略了HTTP verb在路由结构中的重要性。

-- -

Fix 固定

When you use a form , it will default to sending the data via the POST verb. 使用form ,它将默认通过POST动词发送数据。 However, your routes have clearly defined your route as using the PUT verb: 但是,您的路线明确定义了使用PUT动词的路线:

collection do
  put :mass_onboard_result
end

To fix this, you either need to change the route to accept POST responses, or your form to send a PUT request: 要解决此问题,您要么需要更改路由以接受POST响应,要么需要通过form发送PUT请求:

#config/routes.rb
collection do
   match :mass_onboard_result, via [:post, :put]
end

-or- -要么-

#view
<%= form_tag your_path, method: :put %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM