[英]Rails :method=>:patch doesn't work
So, I have this form declaration: 所以,我有这个表格声明:
<%= form_for 'students_list', {:url => update_students_list_stream_url(@stream), :method=>:patch} do |students_list_form| %>
Just as described in API docs, but this leads me to error: 正如API文档中所述,但这导致我出错:
No route matches [POST] "/streams/26/edit-students-list"
So it still tries to post, even though my HTML input has: 因此,即使我的HTML输入具有以下内容,它仍会尝试发布:
<input type="hidden" name="_method" value="patch" />
From Rails guide: 来自Rails指南:
Rails works around this issue by emulating other methods over POST with a hidden input named "_method", which is set to reflect the desired method:
Rails通过使用名为“_method”的隐藏输入在POST上模拟其他方法来解决此问题,该输入设置为反映所需的方法:
I'm quite confused 我很困惑
You'll be better doing this: 你会做得更好:
<%= form_for @stream do |student_form_list| %>
If you've set up your routes using the standard resources
directive , you'll have the following routes: 如果您使用标准
resources
指令设置路线,则您将拥有以下路线:
Of these routes, the update
path should just be students_list_stream_path
-- not the update_students_list_stream_path
you have now. 在这些路由中,
update
路径应该是students_list_stream_path
- 而不是您现在拥有的update_students_list_stream_path
。
If you've set up the form_for
to use the correct object, it will automatically set the path & method for update
. 如果您已将
form_for
设置为使用正确的对象,它将自动设置update
的路径和方法。
I was looking for an answer on why rails loaded method patch as post in the rendered form. 我正在寻找一个答案,为什么rails加载方法补丁作为渲染形式的帖子。 If you ended up here looking for that like I did, this is the answer you are looking for:
如果你像我一样在这里寻找,那么这就是你要找的答案:
https://stackoverflow.com/a/46699512/5750078 https://stackoverflow.com/a/46699512/5750078
From https://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark : 来自https://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark :
Rails use Rack::MethodOverride
middleware that tweaks the HTTP methods PUT/PATCH to POST for supporting old browsers. Rails使用
Rack::MethodOverride
中间件,将HTTP方法PUT / PATCH调整为POST以支持旧浏览器。
This can happen on Rails API application to unload the middleware for performance. 这可能发生在Rails API应用程序上,以卸载中间件以提高性能。 In some cases where you want to call a casual PUT/PATCH request using
form_with
tag in the views, simply add 在某些情况下,如果要在视图中使用
form_with
标记调用临时PUT / PATCH请求,只需添加即可
# config/application.rb
config.middleware.insert_after Rack::Runtime, Rack::MethodOverride
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.