instance method
link_to_function
Ruby on Rails 4.0.13
Since v2.2.3 Last seen in v4.0.13Signature
link_to_function(name, function, html_options={})
Returns a link whose onclick handler triggers the passed JavaScript.
The helper receives a name, JavaScript code, and an optional hash of HTML options. The name is used as the link text and the JavaScript code goes into the onclick attribute. If html_options has an :onclick, that one is put before function. Once all the JavaScript is set, the helper appends “; return false;”.
The href attribute of the tag is set to “#” unless html_options has one.
link_to_function "Greeting", "alert('Hello world!')", class: "nav_link" # => <a class="nav_link" href="#" onclick="alert('Hello world!'); return false;">Greeting</a>
Parameters
-
namereq -
functionreq -
html_optionsopt = {}
Source
# File actionpack/lib/action_view/helpers/javascript_helper.rb, line 105
def link_to_function(name, function, html_options={})
message = "link_to_function is deprecated and will be removed from Rails 4.1. We recommend using Unobtrusive JavaScript instead. " +
"See http://guides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript"
ActiveSupport::Deprecation.warn message
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
Defined in actionpack/lib/action_view/helpers/javascript_helper.rb line 105
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::JavaScriptHelper