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!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570525.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用DreamHost当GoDaddy域名的服务器 步骤

1. 在GoDaddy管理员页面中改变DNS设置&#xff0c;指向DreamHost Name servers&#xff1a; 进入Domain details page: 目前在 SETTINGS -> Nameservers. 数值还是默认值: NS11.DOMAINCONTROL.COM NS12.DOMAINCONTROL.COM 我们需要将它修改为 DreamHost nameservers: …

有用的 Google Analytics Chrome 插件推荐

1.Google Analytics Debugger Debbuger 会列出所有执行的命令以及传过去的参数。 例如&#xff0c;常见的命令有&#xff1a; 初始化 Google Analytics&#xff0c; 传送页面访问 和传送事件。 https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmf…

项目计划定制:项目计划划分与产品项目推进的有机结合

目录&#xff1a; 第一章&#xff1a;导语第二章&#xff1a;需求采集部分的一些要点第三章&#xff1a;项目中一些需要明确的内容第四章&#xff1a;关于成本管理部分第一章&#xff1a;导语 那么什么叫项目&#xff0c;项目计划和项目管理&#xff1f; 项目管理简称为PM&…

Rails 使用 Google Analytics 示例

1. 创建 _google_analytics.html.erb view: <span style"font-size:18px;"><!-- Google Analytics -->window.gawindow.ga||function(){(ga.qga.q||[]).push(arguments)};ga.lnew Date;// Create tracker.ga(create, <% ENV[GOOGLE_ANALYTICS_TRACKI…

开始使用Google Analytics 示例

1. 创建一个 Google Analytics 账户. 2. 创建新的 property: 在左上角点击 Admin。 然后在 Property 标签下面&#xff0c;点击以展开下拉菜单&#xff1a; 选择"create new property": 3. 填写所有必须填写的框: 建议打开 In-page analysis: What is in-page ana…

Windows系统上3种连接Docker虚拟机的方法

1.Docker Quickstart Terminal 打开 "Docker Quickstart Terminal" 然后输入 $ docker-machine ssh <docker machine name> 此处Docker虚拟机的名字是default&#xff1a; 2. gitBash 打开 "gitBash" &#xff0c;然后输入&#xff1a; $ docker-ma…

运行第一个 docker image 并在浏览器中查看

1. SSH to Docker 虚拟机. 打开gitBash或者Docker Quickstart Terminal, 并且输入: <span style"font-size:14px;">$ docker-machine ssh <docker machine name></span>如需要更详细的如何连接Docker Machine的介绍&#xff0c;可以参考我的另一篇…

linux 时间同步ntp

配置前准备:关闭防火墙,配置好hosts,ssh免密登录 1.选定同步的标准,我是以hadoop002(设置为当前时间)作为同步标准,hadoop003(时间是2018年3月21,使用date -s进行设置)与hadoop004(2018年3月21)与之保持同步 在hadoop002上查看是否安装ntp rpm -qa|grep ntp,如果没有显示内容说…

四六级成绩查询,你的『验证码』刷出来了吗?

沉浸在暑假余温里的小可爱们&#xff0c;今天被四六级成绩查询的验证码无情的伤害了一次。 就在8月22日&#xff0c;也就是今天上午9点&#xff0c;学霸们泰然自若&#xff0c;学渣们瑟瑟发抖&#xff0c;有的人甚至在心里考虑是否发微博、朋友圈谢谢超越姐姐&#xff08;此时输…

课时40:类与对象:一些相关的BIF

目录&#xff1a; 一、一些相关的BIF 二、、课时40课后习题及答案 ********************** 一、一些相关的BIF ********************** 1、issubclass&#xff08;class,classinfo&#xff09; 如果第一个参数&#xff08;class&#xff09;是第二个参数&#xff08;classinfo…

Visual Studio Code搭建NodeJs的开发环境

一、Visual Studio Code搭建NodeJs的开发环境 1.下载安装NodeJs并配置环境变量 可以参考&#xff1a;NodeJs的安装和环境变量配置 2.下载安装 VS Code编辑器 可以参考&#xff1a;VsCode插件整理 3.使用Typings工具配置VS Code 的智能提示 可以参考&#xff1a; VS Code智能提示…

Luogu P1280 Niko的任务【线性dp】By cellur925

Nikonikoni~~ 题目传送门 这是当时学长讲dp的第一道例题&#xff0c;我还上去献了个丑&#xff0c;然鹅学长讲的方法我似董非董(??? 我当时说的怎么设计这道题的状态&#xff0c;但是好像说的是二维&#xff0c;本题数据范围均在10000级别&#xff0c;n肯定会空间炸掉的&…

这项技术竟然一个字也不放过!

欢迎大家前往腾讯云社区&#xff0c;获取更多腾讯海量技术实践干货哦~ 本文由腾讯云AI中心发表于云社区专栏 摘要&#xff1a;在日常生活工作中&#xff0c;我们难免会遇到一些问题&#xff0c;比如自己辛辛苦苦写完的资料&#xff0c;好不容易打印出来却发现源文件丢了;收集了…

GoCD notes

Every task in a job is run as an independent program.Changes made by a task to any of its environment variables will not affect subsequent tasks.Changes made by a task on the file system will be visible to subsequent tasks.

C++实现二叉树的相应操作

1. 二叉树的遍历&#xff1a;先序&#xff08;递归、非递归&#xff09;&#xff0c;中序&#xff08;递归、非递归&#xff09;&#xff0c;后序&#xff08;递归、非递归&#xff09;。 #include <iostream> #include <string> #include <stack>using name…

MAC 修改文件夹以及子文件夹和子文件权限 以及 修改文件夹owner

1. 打开Finder&#xff0c;找到想要修改权限的文件夹&#xff0c;打开文件信息对话框。 两种打开方式&#xff1a; 右击文件夹&#xff0c;选择“get info”。 使用“ctrli”。 在弹出对话框底部的“Sharing&Permission” section可以查看文件夹权限信息&#xff1a;N…

windows中使用scrapyd遇到的问题

1.window下‘scrapyd_deploy’不是内部或外部命令&#xff0c;也不是可运行的程序 在windows上使用scrapyd-client 安装后&#xff0c;并不能使用相应的命令scrapyd-deploy 需要在"C:\Python27\Scripts" 目录下 增加scrapyd-deploy.bat文件 内容填充为&#xff1a; …

MAC 设置$PATH 关闭terminal后就失效 解决方案

1 目标 修改$PATH的值 2 问题 在terminal中修改了$PATH&#xff0c;关闭terminal或者换个terminal就不存在了。具体例子&#xff1a; 2.1 查看$PATH当前值 在terminal中输入 echo $PATH2.2 修改$PATH值 此例将/test/folder 加到$PATH的后面。 在terminal中输入 export …

MAC 更新 PHP 指南 以及 PHP常用命令示例

OS: Mac OS X EI Capitan 当前PHP版本&#xff1a;5.5.30 升级后PHP版本&#xff1a;7.0.11 升级步骤参考&#xff1a;https://coolestguidesontheplanet.com/upgrade-php-on-osx/ 1. 安装新版本PHP&#xff1a; 打开terminal&#xff0c;运行&#xff1a; curl -s http://…

MAC 安装 Magento2 community edition 详细步骤

花了好几天时间&#xff0c;终于在Mac OS X EI Capitan上成功安装magento2.1.1. 此处详细记录每个步骤&#xff0c;以备未来使用。 参考magento官方指南&#xff1a;(Easy) Install the Magento archive on your server 1. 确保系统满足prerequisite&#xff1a; 建议不要使用…