16

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?

Thanks.

Don Leatham
  • 2,694
  • 4
  • 29
  • 41
  • Being deprecated means that it is still included, but will be removed in the near future so its use is not recommended. – Andrew Marshall Feb 21 '11 at 07:18
  • 1
    Thanks for the clarification. I still have a concern in that deprecation is not called out in the official docs. Coming in from the Java and PHP community, deprecation was almost always clearly communicated in the official API docs. Doesn't look like that is the case in the Ruby on Rails arena. I was warned that RoR documentation tradition is weak, but the testing ethos is exceptional. So far I'm finding that to be true. :-) – Don Leatham Feb 22 '11 at 05:52
  • 2
    So what's the preferred method of getting the job done? Making a class, and binding jQuery functions to it? – David Krider Jun 01 '12 at 17:27

3 Answers3

26

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))
  end
end
user664833
  • 18,397
  • 19
  • 91
  • 140
holli
  • 1,546
  • 16
  • 17
  • 2
    For anyone finding this in Rails 4.1+... this is still the case -- the link_to_function method is gone! – pdobb Aug 14 '14 at 20:30
15

[Update]

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.

Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
2

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.

nathanvda
  • 49,707
  • 13
  • 117
  • 139