Rails 3 - Status of “link_to_function” method

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.

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.

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?




The link_to_function helper has been deprecated again in 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

# /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))


As TomH mentions, it is now deprecated again in Rails 3.2.4

[Original Answer]

link_to_function is no longer deprecated and is safe for use. Details:

link_to_function was 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:

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.


While I am intending to switch everything over to unobtrusive javascript, a good intermediate step (for me), was to replace

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.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.