Rails 3 - Status of “link_to_function” method

后端 未结 3 1104
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 18:57

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 co

相关标签:
3条回答
  • 2021-01-01 19:16

    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.

    0 讨论(0)
  • 2021-01-01 19:21

    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
    
    0 讨论(0)
  • 2021-01-01 19:22

    [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.

    0 讨论(0)
提交回复
热议问题