Rails 3 - “link_to_function”的状态。方法 [英] Rails 3 - Status of "link_to_function" method
问题描述
在搜索javascript问题的解决方案时,我在Rails 3中看到了关于 link_to_function
的多条评论。但是,我已经能够完成一部分我的基于Rails-3的项目使用 link_to_function
。它工作正常。
In searching for a solution to a javascript problem, I saw multiple comments about link_to_function
being 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.
作为新的Rails,我担心的是我可能会使用长期不支持的东西,或者可能成为传统黑客。在查看api.rubyonrails.org时,我看到 link_to_function
在ActionView :: Helpers :: JavaScriptHelper模块中明确调出,作为Ruby on Rails v3.0.4支持的公共方法。没有关于函数寿命的警告或其他声明。
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_function
clearly 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.
我应该使用其他方法/方法而不是 link_to_function ?或
link_to_function
可以使用吗?
Is there some other approach/method that I should be using instead of link_to_function
in Rails 3? Or is link_to_function
fine to use?
谢谢。
推荐答案
link_to_function
助手已经在3.2.4中再次弃用。
在某些用例中,当您需要调用时,该方法本身非常简单和良好特定的javascript函数等。您可以轻松添加自己的帮助程序以实现相同的功能。以下代码是在 https://github.com/rails/rails中从Jeremy复制的/ pull / 5922#issuecomment-5770442
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
# /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
这篇关于Rails 3 - “link_to_function”的状态。方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!