当点击“link_to”生成的链接时,Rails 3 如何决定使用哪个 HTTP 动词?
我有两个链接:
<%= link_to("Edit", edit_product_path(product.id)) %>
<%= link_to("Delete", product, :method => :delete) %>
生成的链接是:
<a href="/products/81/edit">Edit</a>
<a href="/products/81" data-method="delete" rel="nofollow">Delete</a>
当单击 Edit
和 Delete
时,将使用 GET
方法。
Rails 如何决定使用哪种方法?
Delete
链接中的 data-method="delete"
和 rel="nofollow"
是什么意思?
I have two links:
<%= link_to("Edit", edit_product_path(product.id)) %>
<%= link_to("Delete", product, :method => :delete) %>
The generated links are:
<a href="/products/81/edit">Edit</a>
<a href="/products/81" data-method="delete" rel="nofollow">Delete</a>
When clicking both on Edit
and on Delete
, the GET
method is used.
How Rails decided which method to use ?
What does data-method="delete"
and rel="nofollow"
mean in the Delete
link ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
浏览器通常支持 GET 和 POST HTTP 方法。为了模拟 PUT 和 DELETE 动词,Rails 在提交表单时注入一个特殊的
_method
参数。您可以通过传递
:method
选项来指定要使用的方法,就像您所做的那样。除非指定,否则默认值为
GET
。从 Rails 3 开始,Rails 使用不显眼的 JavaScript 来处理 DELETE 方法。它在
data-method
属性中传递 HTTP 动词,该属性是 HTML 5 功能。在您的情况下,它不起作用,因为您可能忘记包含 JavaScript 库(例如 Prototype 或 jQuery)和 Rails 适配器。
确保您使用的是 jQuery 或 Prototype,并且包含
rails.js
javascript 文件。另外,不要忘记添加
csrf_meta_tag
。如果你想学习 move,我写了一篇关于 的文章几个月前,Rails 3 中的不引人注目的 JavaScript。
Browsers usually support GET and POST HTTP methods. To emulate PUT and DELETE verbs, Rails injects a special
_method
parameter when a form is submitted.You specify the method you want to use by passing the
:method
option, as you did.Unless specified, the default value is
GET
.Starting from Rails 3, Rails uses unobtrusive JavaScript to handle the DELETE method. It passes the HTTP verb in the
data-method
attribute, which is an HTML 5 feature.In your case, it doesn't work because you probably forgot to include a JavaScript library (e.g. Prototype or jQuery) and the Rails adapter.
Make sure you are using either jQuery or Prototype, and you are including the
rails.js
javascript file.Also, don't forget to add the
csrf_meta_tag
.If you want to learn move, I wrote an article about Unobtrusive JavaScript in Rails 3 a few months ago.
有趣的。我可以确认带有“:method => :delete”选项的 link_to 在默认脚手架中确实有效。但是,在尝试构建没有脚手架的项目时,我无法让 link_to 工作。 Button_to 可以正常工作,使用与 link_to 失败的参数相同的参数。
在我的 HEAD 标签中,我有所需的 javascript 包括:
非常混乱。我不知道脚手架发挥了什么“秘密武器”来使 link_to 工作。虽然 Rob 断言 link_to delete 在 Rails 3 中不起作用,在技术上可能不准确,但我发现它实际上是准确的。谢谢罗布,我被困了几个小时才看到你的帖子。
Interesting. I can confirm that link_to with the ":method => :delete" option does work in the default scaffold. However, in trying to build a project without scaffolding, I cannot get link_to to work. button_to works with no problem, using the same parameters that failed with link_to.
In my HEAD tag I have the required javascript includes:
Very confusing. I couldn't figure out what "secret sauce" scaffolding puts into play to make link_to work. While Rob's assertion that link_to delete does not work in Rails 3, may not be technically accurate, I found it practically accurate. Thanks Rob, I had been stuck for hours until I saw your post.
Rails 3 中不能使用链接进行删除
您需要使用一个button_to,例如。
You can't use a link for a delete in rails 3
You need to use a button_to eg.