title: Rails jQuery autocomplete and dynamic nested fields
body: <p>I have an application in which I'm using the gems <code>rails3-jquery-autocomplete</code> and <code>nested_form</code> to suggest existing users to be tagged as contributors to a project entry.</p>
  
  <p>The form view:</p>
  
  <pre><code>&lt;%= f.fields_for :contributors do |contributor|%&gt;
      &lt;%= contributor.autocomplete_field :name, autocomplete_user_profile_last_name_projects_path, :update_elements =&gt; { :user_id =&gt; "#user_id" } %&gt;&lt;br&gt;
      &lt;%= contributor.hidden_field :user_id %&gt;
      &lt;%= contributor.radio_button :contributor_type, 'Colleague' %&gt; 
      &lt;%= contributor.radio_button :contributor_type, 'Supervisor' %&gt; 
      &lt;%= contributor.radio_button :contributor_type, 'Client' %&gt; 
      &lt;%= contributor.link_to_remove "Remove this contributor" %&gt;
  &lt;% end %&gt;
  &lt;%= f.link_to_add "Add a contributor", :contributors %&gt;&lt;br&gt; 
  </code></pre>
  
  <p>I have also overwritten the <code>get_autocomplete_function(parameters)</code> function in order to allow more than one column in my autocomplete, namely the columns <code>first_name</code> and <code>last_name</code></p>
  
  <pre><code>def get_autocomplete_items(parameters)
    items = UserProfile.select("DISTINCT CONCAT_WS(' ', first_name, last_name ) AS full_name, first_name, last_name, id, user_id").where(["CONCAT_WS(' ', first_name, last_name) LIKE ?", "%#{parameters[:term]}%"])
  end
  </code></pre>
  
  <p>I also have the following line in my Projects controller:</p>
  
  <pre><code>autocomplete :user_profile, :last_name, :full =&gt; true, :extra_data =&gt; [:first_name, :last_name, :user_id ], :display_value =&gt; :fullname
  </code></pre>
  
  <p>With the `fullname' function simply being:</p>
  
  <pre><code>def fullname
      "#{first_name} #{last_name}"
  end
  </code></pre>
  
  <p>What I want to do is to get the value of <code>user_id</code> from the autocomplete items and put its value inside the hidden field, but when I check the server the it outputs something like:</p>
  
  <pre><code> Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"DYQ7iJeqdwMQrprSGn0WNHXY5iXDZLA5pUd3OlYJ2so=", "project"=&gt;{"creator"=&gt;"1", "title"=&gt;"lesdodis", "contributors_attributes"=&gt;{"0"=&gt;{"name"=&gt;"Test User the 1st", "user_id"=&gt;"", "contributor_type"=&gt;"Client", "_destroy"=&gt;"false", "id"=&gt;"21"}, "1"=&gt;{"name"=&gt;"Test User the 2nd", "user_id"=&gt;"", "contributor_type"=&gt;"Supervisor", "_destroy"=&gt;"false", "id"=&gt;"22"}, "1393677266696"=&gt;{"name"=&gt;"", "user_id"=&gt;"", "_destroy"=&gt;"1"}, "1393677267518"=&gt;{"name"=&gt;"", "user_id"=&gt;"", "_destroy"=&gt;"1"}}, "description"=&gt;"asdad", "tag_list"=&gt;"sdads", "link"=&gt;"asdasd", "gallery_attributes"=&gt;{"screenshots_attributes"=&gt;{"0"=&gt;{"description"=&gt;"", "_destroy"=&gt;"false", "id"=&gt;"10"}}, "id"=&gt;"16"}}, "commit"=&gt;"Update", "id"=&gt;"16"}
  </code></pre>
  
  <p>where the <code>user_id</code> field inside contributors is left blank. I have already tried manually assigning ids to the fields by using <code>contributor.object.id</code> to assign a unique number to the field name with limited success, as the new dynamically added fields do not have object ids. I am using this form for both creating and updating a project entry, so I would like to ask for help in how to make this work regardless of editing the field or adding a new one.</p>
  
  <p>Update:
  After a while of searching I finally got it to work by adding this code</p>
  
  <pre><code>&lt;% field_name = "#{contributor.object_name}[user_id]".gsub(/(\])?\[/, "_").chop %&gt;
  &lt;%= contributor.autocomplete_field :name, autocomplete_user_profile_last_name_projects_path, :update_elements =&gt; { :user_id =&gt; "##{field_name}" } %&gt;&lt;br&gt;
  </code></pre>
  
  <p>I hope this helps anyone who might encounter the same problem.</p>