# Routing AJAX request from partials to a specific controller

**URL:** <https://www.ruby-forum.com/t/routing-ajax-request-from-partials-to-a-specific-controller/88585>\
**Category:** Rails\
**Created:** [March 12, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/routing-ajax-request-from-partials-to-a-specific-controller/88585 "2007-03-12T21:01:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 12, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/routing-ajax-request-from-partials-to-a-specific-controller/88585/1 "2007-03-12T21:01:36Z")

</div>

Hey 🙂

I’m creating this backend administration interface for a very large  
book database.

I have created, IMHO, a really cool structure of partials that I can  
reuse in different situations, and offer nested editing of the backend  
data, like in this little “use case”:

- The admin does a search on books and is presented with a tabular  
list of books with a short description in each row.  
-=-=- The ‘books’ controller fetches the data, index.rhtml passes the  
books array to a partial that renders the list
- The admin clicks an entry, and an ajax call is made that replaces  
the small descriptive table row with a detailed and editable larger  
presentation of this particular book.  
-=-=- The book has a property, book.series\_participations which is  
like a list of “memberships” with different series of books. This  
array attribute is passed directly to a partial, “/  
series\_participations/backend\_table\_list”, which works in basically  
exactly the same way as the books partial does, that is, it lists all  
participations in a nested table which again allows the admin to click  
and edit a participation directly in the interface without refreshing  
or redirecting. The same partial is also used directly when listing,  
searching and editing SeriesParticipation instances, and indirectly  
(vis-a-vis this book example) in the Series editing module.

So, the series\_participation partials will be used by any of three  
controllers. This is not really any problem until I start using AJAX.  
Editing, saving, deleting, what have you works fine as long as I  
always use absolute paths for rendering the partials and as long as I  
remember to specify the (correct) :controller in the partials.  
Otherwise, the partials would try to commit etc. to the current  
controller, e.g. in this example, if editing a series participation  
opened via the books list, it would try to commit to the :save action  
on the books controller, which would obviously fail.

## Okay, so the problem arises with auto\_complete fields in partials that don’t “belong” to the current controller. I’ve already moved auto\_complete actions into a dedicated module, so that I can load the ones I need for any one controller without duplicating my code. For instance, there’s this one (this is the beginning of my auto\_complete\_for.rb file):

module AutoCompleteFor

## module UserPrintedName def auto\_complete\_for\_user\_printed\_name search = params[:user][:printed\_name] @users = User.find\_by\_sql(“SELECT TOP 25 id, first\_names, last\_name, email FROM users WHERE (first\_names + ’ ’ + last\_name LIKE ‘%#{search.sql\_safe}%’) ORDER BY first\_names ASC, last\_name ASC”) || [] unless search.blank? render :inline =\> “\<%= indexed\_auto\_complete\_result @users, ‘#{self.class.to\_s.gsub(/Controller$/, ‘’).underscore.singularize}\_user\_id’, ‘printed\_name’, ‘id’ %\>” end end

As you can see, I extract the name of the current controller to make  
it possible to add it to different controllers, where the receiving  
HTML elements will have different names due to the different models  
that forms are created for.

This works fine as long as the form that uses a particular  
auto\_completer “matches” the controller that is responsible for the  
form being rendered. That is, if I went into a  
group\_memberships\_controller and included the auto\_completer above,  
then it would work for the form that directly relates to this type of  
model, because it would have a html form element called  
group\_memebership\_user\_id, representing group\_membership.user\_id.

However, consider a Series controller that renders a list of series  
that books might participate in. The admin opens a series in this  
list, which renders a series\_participations partial list to show the  
books that participate in the series, but also renders a form to make  
it possible to create new series\_participations, effectively assigning  
more books to the series in question.

This form will use an auto\_completer for book titles so the admin can  
quickly find the book to include in the series. When the  
auto\_completer executes, it’ll not ask the series\_participations  
controller to get the results, it’ll ask the series controller, as  
that was the one that rendered the form. But this service really  
should be provided by the controller that’ll eventually save the model  
as that is the only one who knows exactly how the form is constructed.  
Even if I included my auto\_completer for this particular case in  
series\_controller, it would try to deliver the results to the wrong  
form elements, as it logically assumes the form elements belong to  
series, not series\_participations.

Okay, I know I’m rambling here, guess that just underlines my  
pickle… Basically, what I want to do, is force these auto\_complete  
request to a particular controller, but I can’t see how to do that.  
observe\_field works, as I can specify the :controller in the url. But  
text\_field\_with\_auto\_complete does not.

Any ideas?

Thanks,  
Daniel 🙂
