# AJAX call chain and DRY questions

**URL:** <https://www.ruby-forum.com/t/ajax-call-chain-and-dry-questions/76928>\
**Category:** Rails\
**Created:** [March 5, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/ajax-call-chain-and-dry-questions/76928 "2006-03-05T17:10:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![last\_resort](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@last\_resort](https://www.ruby-forum.com/u/last_resort)\
**Post date:** [March 5, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/ajax-call-chain-and-dry-questions/76928/1 "2006-03-05T17:10:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![last\_resort](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@last\_resort](https://www.ruby-forum.com/u/last_resort)\
**Post date:** [March 5, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/ajax-call-chain-and-dry-questions/76928/2 "2006-03-05T17:16:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![last\_resort](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@last\_resort](https://www.ruby-forum.com/u/last_resort)\
**Post date:** [March 6, 2006, 3:58pm UTC](https://www.ruby-forum.com/t/ajax-call-chain-and-dry-questions/76928/3 "2006-03-06T15:58:37Z")

</div>

On 3/5/06, last resort [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://lists.rubyonrails.org/mailman/listinfo/rails](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.

–
