当点击“link_to”生成的链接时,Rails 3 如何决定使用哪个 HTTP 动词?

发布于 2024-10-07 21:09:18 字数 547 浏览 5 评论 0原文

我有两个链接:

<%= 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>

当单击 EditDelete 时,将使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫以 2024-10-14 21:09:18

浏览器通常支持 GET 和 POST HTTP 方法。为了模拟 PUT 和 DELETE 动词,Rails 在提交表单时注入一个特殊的 _method 参数。

您可以通过传递 :method 选项来指定要使用的方法,就像您所做的那样。

<%= link_to("Action with DELETE", path_to_something, :method => :delete) %>
<%= link_to("Action with PUT", path_to_something, :method => :put) %>

除非指定,否则默认值为 GET

从 Rails 3 开始,Rails 使用不显眼的 JavaScript 来处理 DELETE 方法。它在 data-method 属性中传递 HTTP 动词,该属性是 HTML 5 功能

在您的情况下,它不起作用,因为您可能忘记包含 JavaScript 库(例如 Prototype 或 jQuery)和 Rails 适配器。

确保您使用的是 jQuery 或 Prototype,并且包含 rails.js javascript 文件。
另外,不要忘记添加 csrf_meta_tag

<%= 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.

<%= link_to("Action with DELETE", path_to_something, :method => :delete) %>
<%= link_to("Action with PUT", path_to_something, :method => :put) %>

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.

<%= csrf_meta_tag %>

If you want to learn move, I wrote an article about Unobtrusive JavaScript in Rails 3 a few months ago.

咆哮 2024-10-14 21:09:18

有趣的。我可以确认带有“:method => :delete”选项的 link_to 在默认脚手架中确实有效。但是,在尝试构建没有脚手架的项目时,我无法让 link_to 工作。 Button_to 可以正常工作,使用与 link_to 失败的参数相同的参数。

在我的 HEAD 标签中,我有所需的 javascript 包括:

javascript_include_tag :defaults
csrf_meta_tag

非常混乱。我不知道脚手架发挥了什么“秘密武器”来使 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:

javascript_include_tag :defaults
csrf_meta_tag

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.

梦情居士 2024-10-14 21:09:18

Rails 3 中不能使用链接进行删除
您需要使用一个button_to,例如。

<%= button_to("Delete", product, :method => :delete) %>

You can't use a link for a delete in rails 3
You need to use a button_to eg.

<%= button_to("Delete", product, :method => :delete) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文