日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Rails用DELETE method提交表单讲解

發布時間:2025/3/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Rails用DELETE method提交表单讲解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Sometimes we need to submit form?using methods other than ‘post’, in this example, it’s ‘delete’.

For example, I wanted to delete a user when a form is submitted with the user name and ‘delete’ method.

1. I created the route like this:

delete ‘test url’, to: ‘users#destroy’
So I will handle the delete function in 'users' controller, 'destroy' action.


2.1 The view code:

<%= form_tag(‘test url’, method: :delete) do %><%= label_tag ‘Please enter the username that you want to delete:’ %><%= text_field_tag :name %><%= submit_tag ‘Send delete request’ %> <% end %>
2.2 This view?code generates the following html code:

<form action=”test url” accept-charset=”UTF-8″ method=”post”><input name=”utf8″ type=”hidden” value=”?” /><input type=”hidden” name=”_method” value=”delete” /><input type=”hidden” name=”authenticity_token” value=”5Rj1osaYdsOkH94pyig99l5Ud64U1H25LCuK33plQuf2Lg+a/+Ub6VVlYBSOvDotOIrX3SCKB6mYsfZTyPkI+Q==” /><label for=”Please_enter_the_username_that_you_want_to_delete:”>Please enter the username that you want to delete:</label><input type=”text” name=”name” id=”name” /><input type=”submit” name=”commit” value=”Send delete request” /> </form>


As you can see above, the form method is still ‘post’. I was really confused at this point, since the method didn’t?turn out to be the ‘delete’ method I expected.

But actually rails generated?a hidden input called _method that is supposed to carry the intended verb for the server to interpret. In this case, it’s ‘delete’, exactly what I need.


3. Now let’s see the view, it looks like this:


I typed ‘test user name’ and hit ‘Send delete request’ button, it sent out this http request:


The request method is ‘Post’, with form data ‘_method’ set as ‘delete’ which sent my request to 'users' controller, 'destroy' action.?

Mission complete!

總結

以上是生活随笔為你收集整理的Rails用DELETE method提交表单讲解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。