Rails 3 - Status of “link_to_function” method

后端 未结 3 1105
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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
    

提交回复
热议问题