获取要使用的表单:method => :删除(导轨) [英] Getting a form to use :method => :delete (rails)

查看:141
本文介绍了获取要使用的表单:method => :删除(导轨)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多line_items的购物车。我希望在每个订单项旁边都有一个删除按钮,点击后可从购物车中删除line_item。

I have a cart which contains many line_items. I'd like to have a "delete" button next to each line item that, upon clicked, removes the line_item from the cart.

我知道我可以用button_to方法做到这一点,但我想使用form_for,因为我想要更改line_item的父对象的属性同一时间(每条line_item也属于一门课程,我想告诉课程父母它已不在购物车中)。

I know I can do this with a button_to method, but I'd like to use form_for because I'd like to change the attributes of the line_item's parent object at the same time (each line_item also belongs to a course, and I'd like to tell the course parent that it's no longer in the cart).

这是我使用form_for的代码:

Here's my code using form_for:

<%= form_for(line_item, :method => :delete, :remote => true) do |f| %>
<%= f.submit :value => "Delete" %>
<% end %>

ruby​​文档说简单地添加:method =>:delete应该可以工作(http:// api .rubyonrails.org / classes / ActionView / Helpers / FormHelper.html#method-i-form_for),但是呈现的html不太正确。它还是

The ruby documentation says that simply adding :method => :delete should work (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for), but the rendered html isn't quite right. It's still

<input name="_method" type="hidden" value="put">

但应该是:

But it should be:

<input name="_method" type="hidden" value="delete">

我做错了什么?

推荐答案

Mark Needham有一个博客文章,讨论为什么:method =>在 form_for 中删除​​不起作用。他说

Mark Needham has a blog post that talks about why :method => delete in form_for doesn't work. He says


事实证明'form_for'期望':method'作为右手的一部分提供
大部分参数作为哈希键的一部分:'html'。

It turns out that ‘form_for’ expects the ‘:method’ to be provided as part of the right hand most argument as part of a hash with the key ‘:html’.

所以你需要改变你的代码:

So you need to change your code from:

<%= form_for(line_item, :method => :delete, :remote => true) do |f| %>

到:

<%= form_for(line_item, :html => { :method => :delete, :remote => true }) do |f| %>

我在Rails 3.0应用程序中试过了,生成的HTML是:

I tried it in a Rails 3.0 application, and the generated HTML was:

<input type="hidden" value="delete" name="_method">

这篇关于获取要使用的表单:method =&gt; :删除(导轨)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆