Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where did link_to_function disappear to in Rails 3?

Tags:

I was just playing around with Rails 3 beta and noticed that link_to_function is now gone. I presume there's an alternate method of achieving the same result (onclick event?) but I was wondering if there's a more Rails-3'y way of doing it. TIA.

like image 614
Psynix Avatar asked Feb 12 '10 07:02

Psynix


2 Answers

Rails 3 seems to have done away with Prototype Helper in favour of a less obtrusive/JS library agnostic approach. The goal is to eliminate all inline javascript generated by Rails. Prototype Helper generated pretty much all of the javascript.

Now any of the non remote variants of helpers will generate the proper javascript for a remote call in your JS library of choice just by supplying the :remote => true option.

Unfortunately this doesn't hold true for the x to function methods. For the time being there are the prototype legacy helpers which are no longer a core part of Rails.

You could also use call as defined in ActionView::Helpers::PrototypeHelper::JavascriptGenerator::GeneratorMethods to supply javascript code to :onclick as an html_option for link_to, but that's not exactly pretty.

Examples:

Rails < 3                      | Rails 3 link_to_remote "target", url   | link_to "target", url, :remote => true form_remote_for @post          | form_for @post, :remote => true 

etc....

Or something to that effect. I'm having trouble finding official documentation to back up my claims. So the release notes will have to do for now.

Before you can use it, you'll need to include the proper js source files. Ensure that you are loading prototype.js and rails.js, or the library and driver for JS framework of choice in their place.

Remember, Rails 3 is in beta now. But that doesn't mean it's done. I honestly have no idea how link_to_function will be replaced. It goes against the ideal of unobtrusive javascript.

like image 78
EmFi Avatar answered Nov 19 '22 23:11

EmFi


To answer my own question, seems this works and is sufficient for what I need:

link_to "name", nil, :onlick => "alert('Hello, world!')" 
like image 39
Psynix Avatar answered Nov 19 '22 23:11

Psynix