Rails用DELETE method提交表单讲解
2015-07-19 08:51
435 查看
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:
So I will handle the delete function in 'users' controller, 'destroy' action.
2.1 The view code:
2.2 This view code generates the following html code:
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!
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!
相关文章推荐
- 红宝石(Ruby)史话
- 在线用表单建立文件夹
- Jquery 表单取值赋值的一些基本操作
- Ruby简明教程之方法(Method)介绍
- 浅谈Rails 4 中Strong Parameters机制
- 简单对比分析Ruby on Rails 和 Laravel
- oracle数据库删除数据Delete语句和Truncate语句的使用比较
- 在 Ubuntu 12.04 Server 上安装部署 Ruby on Rails 应用
- Ruby on Rails下的图像处理入门教程
- delete from 表名与truncate table 表名区别
- drop,truncate与delete的区别
- mysql delete limit 使用方法详解
- MySQL DELETE语法使用详细解析
- ORACLE SQL-UPDATE、DELETE、INSERT优化和使用技巧分享
- sqlserver中delete、update中使用表别名和oracle的区别
- rails创建应用程序实例
- SQL Server中的XML数据进行insert、update、delete
- 浅析drop user与delete from mysql.user的区别
- javascript 表单的友好用户体现
- SQL Server中的XML数据进行insert、update、delete操作实现代码