Passing a form object to a partial within a fields_for using observe_field

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

<%= f.fields_for :collaborations do |builder| %> <%= render 'collaboration_fields', :f => builder %> <% end %>

<%= 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:

Search for collaborator: <%= render :partial => 'live_search' %> <%= link_to_remove_fields "Remove collaborator", f %>
<%= observe_field "live_user_search_form", :url => { :controller => 'articles', :action => 'live_search' }, :frequency => 0.5, :update => 'live_search_results', :with => "'search_text=' + escape(value)", :loading => "document.getElementById('spinner').style.display='inline'", :loaded => "document.getElementById('spinner').style.display='none'" %>

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 %>
<%= :collaborator_id, { |hit| ["#{hit.instance.first_name}
#{hit.instance.last_name} (#{})",] }, { :include_blank => true } %>
<% else %>
<%= :collaborator_id, { |hit| ["#{hit.first_name}
#{hit.last_name} (#{})",] }, { :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|
@live_search_users = User ) do
keywords terms
render :partial => ‘/articles/live_search’
render :partial => ‘/articles/live_search’

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs