Using a helper within a controller

Shandy Nantz wrote:

I would like to use the link_to_remote method in my controller so I can
update a list within my .rhtml file without having to re-load the entire
page. Basically, what I am doing is adding stuff to a list and then
allowing that stuff to be deleted from the list if the user so chooses -
all with the built-in AJAX functionality. The idea is that I do not want
the entire page to re-load. I'm thinking that there is an eval() method
that I can use but have not yet discovered it. Anyone have any ideas?

Put the snip of rHTML that needs to refresh into a partial. Include it the normal way, like this:

  <div id='refresh_me'>
     <%= render :partial => 'my_partial' %>
  </div>

Now wire link_to_remote up to an action that looks like this:

  def my_action
     return unless request.xhr?
     render :update do |page| # <-- I call that rjs sometimes!
        page.replace_html 'refresh_me', :partial => 'my_partial'
     end
  end

The deal is that almost* anything you can pass to render, you can also pass to the second argument to JavaScriptGenerator#replace_html.

The first code injects raw HTML into your page as it renders, before it goes over the wire. The second snip renders the partial, then creates JavaScript containing Element.update('refresh_me', '<my partial html codes>'). This goes over the wire, and the Ajax handlers from prototype.js will replace the innerHTML member of that <div id='refresh_me'> with the new version.

(Question for the lifers - is it _really_ "almost anything"? Or is it "anything"!?)