Ben
Ben

Reputation: 1349

How can I re-populate a list in a <div> after adding an item to it using AJAX?

Specifically, I have a number of pages in my Rails app that use the same partial. In the action handler for each page I create an array object (e.g. @list_elements) based on a database query. Each page uses a different query so that each page has different list elements in it. At the top of each page I have a form_remote_tag containing an edit field, allowing the user to add a new element in a dynamic, AJAXy fashion (think something like Twitter 'What's happening' box).

My problem is that when the AJAX command fires I need to reload the list to include the newly added item, but the contents of the list were determined by a database query. I need to remember which query applies to the current page (i.e. controller action) so that I can run it again. I thought about storing something in the rails session structure but it seems like overkill - it's like storing the current page all the time.

Anybody done anything like this and have a nice Railsy way to achieve it?

Ben

Upvotes: 0

Views: 113

Answers (1)

Audie
Audie

Reputation: 193

Couldn't you just re-render the partial in your rjs template?

page[:div_element].replace_html :partial => 'partial'

If you perform the query and define the array in the controller action, then an ajax call will refresh that array.

Upvotes: 1

Related Questions