Method: ActionView::Helpers::JavaScriptHelper#link_to_function
- Defined in:
- lib/action_view/helpers/javascript_helper.rb
#link_to_function(name, *args, &block) ⇒ Object
Returns a link that will trigger a JavaScript function using the onclick handler and return false after the fact.
The function argument can be omitted in favor of an update_page block, which evaluates to a string when the template is rendered (instead of making an Ajax request first).
Examples:
link_to_function "Greeting", "alert('Hello world!')"
Produces:
<a onclick="alert('Hello world!'); return false;" href="#">Greeting</a>
link_to_function(image_tag("delete"), "if (confirm('Really?')) do_delete()")
Produces:
<a onclick="if (confirm('Really?')) do_delete(); return false;" href="#">
<img src="/images/delete.png?" alt="Delete"/>
</a>
link_to_function("Show me more", nil, :id => "more_link") do |page|
page[:details].visual_effect :toggle_blind
page[:more_link].replace_html "Show me less"
end
Produces:
<a href="#" id="more_link" onclick="try {
$("details").visualEffect("toggle_blind");
$("more_link").update("Show me less");
}
catch (e) {
alert('RJS error:\n\n' + e.toString());
alert('$(\"details\").visualEffect(\"toggle_blind\");
\n$(\"more_link\").update(\"Show me less\");');
throw e
};
return false;">Show me more</a>
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/action_view/helpers/javascript_helper.rb', line 82 def link_to_function(name, *args, &block) = args.last.is_a?(Hash) ? args.pop : {} function = args[0] || '' .symbolize_keys! function = update_page(&block) if block_given? content_tag( "a", name, .merge({ :href => [:href] || "#", :onclick => ([:onclick] ? "#{[:onclick]}; " : "") + "#{function}; return false;" }) ) end |