Rebinding UJS actions in Rails 3.2 application
In a recent rails 3.2 application I am refactoring, some of the js actions had to be redefined to use the 'on' event rather than 'live' as that is deprecated since jquery 1.7.
By doing so some of the events stopped working immediately. This is due to the fact that the 'on' event has to be defined on the document body rather than the individual elements in question.
The example I have provided is a simple example of liking a blog post. The user would see a 'like' link within a blog post and upon clicking on the button it would call a UJS action within the rails application which has a corresponding JS template containing certain actions that must be triggered to update the view. If we change the implementation to define 'on' event for the like link element once the view gets updated the event is lost until a page refresh.
If we define it in the context of the document object it persists and no other changes need to be done to the remaining js or UJS template code.
For further information on how to rewrite 'live' actions to 'on' please refer to the 'live' documentation on the jquery website:
http://api.jquery.com/live/
Below is a gist of the code in question mentioned above.
https://gist.github.com/cheeyeo/6435628















