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:

  <div class="article_form_collaborators">
        <%= f.fields_for :collaborations do |builder| %>
                <%= render 'collaboration_fields', :f => builder %>
        <% end %>
  </div>
  <p><%= link_to_add_fields "Add another collaborator",
f, :collaborations %></p>


The collaboration_fields partial includes an observe_field (obtrusive
as it may be...) call to handle the live search functionality:

<div class="collaborator_fields">
        Search for collaborator: <input type="text"
id="live_user_search_form" name="search" />
        <span id="live_search_results">
                <%= render :partial => 'live_search' %>
        </span>
        <img id="spinner" src="/images/spinner.gif" style="display: none;" /
>
        <%= link_to_remove_fields "Remove collaborator", f %>
</div>
<%= 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 %>
        <%= 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!

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to