How to create a delete form with RESTful routes in Rails.

血红的双手。 提交于 2019-12-06 11:10:24
ahmy

I think that will already work. As in rails guide http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

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:

as you see in the output form there is

<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
    ....
    <input name="_method" type="hidden" value="delete" />
    ....
</form>

When reading this variable, rails understood that this is a DELETE method, not a POST method. even though the form it self is a POST.

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