javascript Rails 3 - “link_to_function”方法的状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5063246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 3 - Status of "link_to_function" method
提问by Don Leatham
In searching for a solution to a javascript problem, I saw multiple comments about link_to_functionbeing deprecated in Rails 3. However, I've been able to complete a section of my Rails-3-based project using link_to_function. It works fine.
在寻找 javascript 问题的解决方案时,我看到了多条关于link_to_function在 Rails 3 中被弃用的评论。但是,我已经能够使用link_to_function. 它工作正常。
Being new Rails, my concern is that I might be using something that is not going to be supported over the long-term or could become a legacy hack. In looking at api.rubyonrails.org, I see link_to_functionclearly called out in the ActionView::Helpers::JavaScriptHelper module as a supported public method for Ruby on Rails v3.0.4. No warnings or other statements about the longevity of the function.
作为一个新的 Rails,我担心我可能会使用一些不会得到长期支持的东西,或者可能会成为遗留的 hack。在查看 api.rubyonrails.org 时,我link_to_function清楚地看到ActionView::Helpers::JavaScriptHelper 模块中作为 Ruby on Rails v3.0.4 支持的公共方法。没有关于函数寿命的警告或其他声明。
Is there some other approach/method that I should be using instead of link_to_functionin Rails 3? Or is link_to_functionfine to use?
我应该使用其他一些方法/方法而不是link_to_function在 Rails 3 中使用吗?或者link_to_function可以用吗?
Thanks.
谢谢。
回答by holli
The link_to_functionhelper has been deprecated again in 3.2.4.
该link_to_function助手在 3.2.4 中再次被弃用。
The method itself is quite simply and good in some use cases when you need to call specific javascript function etc. You can easily add your own helper to achieve the same functionality. The following code was copied from Jeremy in https://github.com/rails/rails/pull/5922#issuecomment-5770442
当您需要调用特定的 javascript 函数等时,该方法本身非常简单且很好。您可以轻松添加自己的帮助程序来实现相同的功能。以下代码是从 Jeremy 在https://github.com/rails/rails/pull/5922#issuecomment-5770442 中复制的
# /app/helpers/link_to_function_helper.rb
module LinkToFunctionHelper
def link_to_function(name, *args, &block)
html_options = args.extract_options!.symbolize_keys
function = block_given? ? update_page(&block) : args[0] || ''
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
href = html_options[:href] || '#'
content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick))
end
end
回答by Michelle Tilley
[Update]
[更新]
As TomH mentions, it is now deprecated againin Rails 3.2.4
正如 TomH 所提到的,它现在在 Rails 3.2.4 中再次被弃用
[Original Answer]
[原答案]
link_to_functionis no longer deprecatedand is safe for use. Details:
link_to_function是不再过时,是可以安全使用。细节:
link_to_functionwas slated to be removed in Rails 3 (and was for a while when Rails 3 was in beta), but the function was put back in https://github.com/rails/rails/commit/d69e561. Several people wondered (aloud, in the comments of the commit) why the function was put back, when the big push in Rails 3 was for Unobtrusive JavaScript, so DHH chimed in:
link_to_function原定在 Rails 3 中删除(并且在 Rails 3 处于测试阶段时有一段时间),但该功能被放回了https://github.com/rails/rails/commit/d69e561。有几个人想知道(在提交的评论中大声说)为什么这个函数被放回去了,当时 Rails 3 的大推动是为了Unobtrusive JavaScript,所以 DHH 插话说:
This is to handle non-generic function triggers explicitly in ERb when you're not inclined to manually hook it up through JS. The big win for UJS in Rails 3 is getting rid of big swaths of boilerplate code ala data-confirm and data-remote. These generics are auto-wired and you don't have to bother with it.
But for your own functions, ala link_to_function "Add calendar", "Calendar.add()", this is a more direct, immediate way to go. If you still would rather go through an external JS and wire it up by hand through dom:ready, have a field day.
The support for UJS in Rails 3 is not about being dogmatic. Just like the support for REST isn't. We'll expose the major value to everyone and then allow for outlets where it makes since. Here, it makes sense.
This, btw, is not about prototype or any other specific js framework.
这是为了在您不倾向于通过 JS 手动连接它时,在 ERb 中显式处理非通用函数触发器。UJS 在 Rails 3 中的一大胜利是摆脱了大量的样板代码 ala data-confirm 和 data-remote。这些泛型是自动连接的,您不必理会它。
但是对于您自己的函数,ala link_to_function“添加日历”、“Calendar.add()”,这是一种更直接、更直接的方法。如果您仍然希望通过外部 JS 并通过 dom:ready 手动将其连接起来,那么请进行实地考察。
Rails 3 中对 UJS 的支持不是教条的。就像对 REST 的支持不是一样。我们将向每个人展示主要价值,然后允许其销售。在这里,这是有道理的。
顺便说一句,这与原型或任何其他特定的 js 框架无关。
回答by nathanvda
While I am intending to switch everything over to unobtrusive javascript, a good intermediate step (for me), was to replace
虽然我打算将所有内容都切换到不显眼的 javascript,但一个很好的中间步骤(对我来说)是替换
link_to_function icon_tag('close.png'), '$(this).parent().hide()', :title => t('actions.close'), :class => 'close'
with the following:
具有以下内容:
link_to icon_tag('close.png'), '#', :onclick => '$(this).parent().hide()', :title => t('actions.close'), :class => 'close'
Very simple. Hope this helps.
很简单的。希望这可以帮助。

