I’m a Rails newbie, so please bear with me. I’m using version 3.0.0.
I have a form to create and update articles which uses a fields_for to
nest a ‘collaborator’ resource, of which an article :has_many. I
would like for users to be able to search for other users to add as
collaborators (co-authors of the article) using a live search field
linked to a select box (or something similar).
Unfortunately, I need to pass the original article form object to the
search results partial on every AJAX request in the live search. Is
there any simple way to do this, or a better approach entirely?
Here’s my code:
Following the nested model Railscast, my articles form includes this
div:
<%= link_to_add_fields "Add another collaborator", f, :collaborations %>
The collaboration_fields partial includes an observe_field (obtrusive
as it may be…) call to handle the live search functionality:

And the live_search partial just allows the user to select from the
search results:
<% if @live_search_users && @live_search_users.hits.count > 0 %>
<%= f.select :collaborator_id,
@live_search_users.hits.map { |hit| ["#{hit.instance.first_name}
#{hit.instance.last_name} (#{hit.instance.email})",
hit.instance.id] }, { :include_blank => true } %>
<% else %>
<%= f.select :collaborator_id,
current_user.friends.map { |hit| ["#{hit.first_name}
#{hit.last_name} (#{hit.email})", hit.id] }, { :include_blank =>
true } %>
<% end %>
The searching is handled through a live_search function in the
articles controller:
def live_search
if request.xhr?
if params[‘search_text’].strip.length > 0
terms = params[‘search_text’].split.collect do |word|
“%#{word.downcase}”
end
@live_search_users = Sunspot.search( User ) do
keywords terms
end
render :partial => ‘/articles/live_search’
else
render :partial => ‘/articles/live_search’
end
end
end
As you can see the live_search partial requires the article form, f,
for the select function, but obviously that’s not available unless
passed somehow. I’ve tried passing it around using :locals in every
place I can think of, such as from the observe_field call through the
controller and back to the partial, but nothing seems to work.
Any help or guidance would be greatly appreciated!