Rails UJS for dynamically added content
I am using Rails 3.0 with a prototype ujs rails.js script.
I have links link_to "...", :remote => truethat are added to the page after the page loads, and so after rails.jsthat the ujs events are attached to all the links a[data-remote].
How do I get rails.js to rescan new links?
rails.js seems to consist of a single anonymous function, so I cannot name it directly.
The only way I can do this is to wrap all rails.js in my own function so that I can then call the page load * and * when adding new links.
This seems like a reasonable approach, but I am embarrassed because I cannot believe that I am the first to have this problem and that there is no standard way to do this ...
When you show the button, link, etc., you use the rails helpers
= link_to "Add to basket", basket_path, :method => :post, :remote => true
So, in response to your remote view (for example: some_view.js.erb / some_view.js.haml ), you must use the rail helpers again if you want : the remote element . You do not need to rescan their new DOM elements to enable remote behavior.
In js view you can update / replace contents from DOM with javascript
:plain
$('#some-id-selector').replaceWith("#{escape_javascript(render :partial => 'some_other_view.html.haml')}");
You can also look in the network inspector of your browser if new actions are sent.