AJAX call chain and DRY questions

Hey

I have a page with a drop list. When the user selects an option in the
drop I call an action in the controller with ajax. Now, in the
controller, I gather some data from the db, but how do I present these
data in a nice way? I really need to make use of the html helpers, but
these clearly are not available in the controller. How do I output the
array that I have gathered?

My view page looks like this:

<%= javascript_include_tag “prototype” %>
<%
@genre
%>
<%= start_form_tag :action => ‘index’ %>

<%=select(:genre, :id, @genres2)%>
<%= end_form_tag %>

<%= observe_field(“genre[id]”,
:frequency => 0.25,
:update => “identities_box”,
:url => { :action => :view_genre },
:with => “‘genre_id=’+value”) %>

Identiteter

<% for identity in @identities %> <% end %>
Namn Beskrivning Bild
<%= link_to (h identity.name), :action => "view_identity", :id => identity %> <%=h identity.description %>

__________________________________

In the controller I have the action view_genre defined, called when user
selects an option in the drop list:

def view_genre()
@identities = Identity.available_identities(params[‘genre_id’])
#
#Here I would like to output the array with identities in a
table just like I did when the view loaded in the users browser. Problem
is, I dont know how to do it and still make use of the handy html
helpers.
#
end


This brings me to the question about DRY (dont repeat yourself). If I in
the action “view_genre” in the controller also writes how the table
should be rendered, then I have the table definition in two places, both
controller and view, that cant be right.

I guess I have to pass the array to the view in som way when the ajax
call is coming in to the controller, and then let the view create the
updated table… but, I dont know how. Please, I need some help here.

hmmm, after thinking a bit more on this I guess partials would be a good
thing for solving my problem. But if there is a better way, please let
me know.

On 3/5/06, last resort [email protected] wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails

Yes, partials are the way I handled this. The advantages I see are that
the
HTML stays in the view, logic in the controller, and you can unit-test a
partial. Trust rails & prototype to correctly handle the AJAX plumbing
:slight_smile:
The only disadvantage that I found was naming 4 partials on a page that
results from multple AJAX handlers.