Forum: Ruby on Rails AJAX call chain and DRY questions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
last resort (Guest)
on 2006-03-05 18:10
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' %>
<div id="genre_id_box">
<%=select(:genre, :id, @genres2)%>
</div>
<%= end_form_tag %>

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

<h1>Identiteter</h1>
<div id="identities_box">
<table>
  <tr>
    <td>Namn</td>
    <td>Beskrivning</td>
    <td>Bild</td>
  </tr>
<% for identity in @identities %>
  <tr>
    <td><%= link_to (h identity.name), :action => "view_identity", :id
=> identity %></td>
    <td><%=h identity.description %></td>
    <td><img src="<%=h identity.imagepath %>" /></td>
  </tr>
<% end %>
</table>
</div>
<br />
__________________________________

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.
last resort (Guest)
on 2006-03-05 18:16
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.
Peter F. (Guest)
on 2006-03-06 16:58
(Received via mailing list)
On 3/5/06, last resort <removed_email_address@domain.invalid> 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
:)
The only disadvantage that I found was naming 4 partials on a page that
results from multple AJAX handlers.


--
This topic is locked and can not be replied to.