加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 当点击“link_to”生成的链接时,Rails 3如何决

发布时间:2020-12-17 02:59:26 所属栏目:百科 来源:网络整理
导读:我有两个链接: %= link_to("Edit",edit_product_path(product.id)) %%= link_to("Delete",product,:method = :delete) % 生成的链接是: a href="/products/81/edit"Edit/aa href="/products/81" data-method="delete" rel="nofollow"Delete/a 单击“编辑”
我有两个链接:

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

单击“编辑”和“删除”时,将使用GET方法.

Rails如何决定使用哪种方法?

在删除链接中,data-method =“delete”和rel =“nofollow”是什么意思?

解决方法

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

您可以通过传递:method选项指定要使用的方法,就像您一样.

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

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

从Rails 3开始,Rails使用不引人注目的JavaScript来处理DELETE方法.它在data-method属性中传递HTTP谓词,该属性为HTML 5 feature.

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

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

<%= csrf_meta_tag %>

如果你想学习移动,几个月前我写了一篇关于Unobtrusive JavaScript in Rails 3的文章.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读