Rails 4 not changing post method to patch

丶灬走出姿态 提交于 2019-12-06 07:15:31

To add a bit of specificity, to Tashow's answer above (which set me on the right track), I had some hidden fields that look'd like this, in a nested form.:

<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", nil) %>
<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", '') %>

Once I got rid of these, everything began working properly again. (There still remained similar-looking <input> tags generated by fields_for, etc.)

After some more trial and error, I realised I had left some plain input tags in a deeper nested level of the form (instead of going with the normal fields_for and separate builders for each level). I guess that somehow screwed up the relations and affected the method of the parent form.

That was such a mind blending mess up.

Just to answer your question from title, I think your form method is "PATCH" indeed. Refer to the guide http://guides.rubyonrails.org/form_helpers.html about how rails makes a patch form.

The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PATCH" and "DELETE" requests (besides "GET" and "POST"). However, most browsers don't support methods other than "GET" and "POST" when it comes to submitting forms.

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:

form_for takes an object as first argument, and its usually better to keep the REST-like way of rails handling the update method.

The action of you html form displays "/classrooms/23/edit" so yes it won't work.

form_for(@classroom, url: update_classroom_path(@classroom), method: "patch")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!