Rails 3 的“data-method='delete'"是如何实现的优雅地降级? [英] How Does Rails 3's "data-method='delete'" Degrade Gracefully?

查看:48
本文介绍了Rails 3 的“data-method='delete'"是如何实现的优雅地降级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3 做了一些很酷的事情来让 Javascript 不引人注目,所以他们做了这样的事情:

Rails 3 does some cool stuff to make Javascript unobtrusive, so they've done things like this:

= link_to "Logout", user_session_path, :method => :delete

.. 转换为

<a href="/logout" data-method="delete" rel="nofollow">Logout</a>

但我突然想到.. 当我关闭 javascript 时,该方法不再是 DELETE,而是 GET.那么是否有计划或有某种方法允许这些 data- 属性优雅地降级,以便该链接仍然是 DELETE 请求?

But it just occurred to me.. When I turn off javascript the method isn't DELETE anymore, it's GET as expected. So are there plans to, or is there some way to, allow these data- attributes to degrade gracefully, so that link still is a DELETE request?

推荐答案

他们在 Rails 3 中对这些 data- 属性所做的更改不是为了优雅降级,而是为了不引人注目的 JavaScript.

The change they made in Rails 3 with these data- attributes wasn't about graceful degradation, it was about unobtrusive JavaScript.

在 Rails 2 中,指定 :method =>:delete 在链接上会生成一大堆内联 JavaScript,这些 JavaScript 会创建一个带有隐藏输入的表单,然后提交表单.这和现在一样:关闭 JavaScript,它默认为 GET 请求.因此,支持没有 JavaScript 的情况与之前相同.

In Rails 2, specifying :method => :delete on a link would generate a whole bunch of inline JavaScript that would create a form with a hidden input and then submit the form. That was the same as it is now: turn off JavaScript and it defaults to a GET request. As such, supporting the case of no JavaScript is the same as it was before.

一种选择是使用表单/按钮而不是链接,这样您就可以将该方法作为隐藏字段包含在内,就像 Rails 2 JavaScript 所做的那样.另一种选择是让 GET 版本将您带到一个中间页面,该页面又具有表单/按钮.

One option is to use a form/button instead of a link so you can include the method as a hidden field, much like the Rails 2 JavaScript does. Another option is to have the GET version take you to an intermediate page which in turn has the form/button.

新方法的好处是不引人注目.用于更改 HTTP 动词的 JavaScript 存在于外部文件中,并使用 data- 属性来确定它应该附加到哪些元素.

The benefit of the new approach is that it's unobtrusive. The JavaScript for changing the HTTP verb exists in an external file and uses the data- attributes to determine which elements it should be attached to.

这篇关于Rails 3 的“data-method='delete'"是如何实现的优雅地降级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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