<?xml version='1.0' encoding='utf-8' ?>
<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/'>
  <channel>
    <title>Wolfmans Howlings: Getting a record id from text_field_with_auto_complete</title>
    <link>http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text_field_with_auto_complete</link>
    <description>A programmers Blog about Ruby, Rails and a few other issue</description>
    <language>en-us</language>
    <ttl>40</ttl>
    <item>
      <title>Getting a record id from text_field_with_auto_complete</title>
      <description>
        &lt;p&gt;I ran into this problem a few times,and I have seen others asking the
        same question, if you use &lt;code&gt;text_field_with_auto_complete&lt;/code&gt; and the
        selection list returns non-unique results, how do you reference the
        actual record in the database you want?&lt;/p&gt;
        
        &lt;p&gt;For instance if you have &lt;code&gt;text_field_with_auto_complete :customer, :name&lt;/code&gt;
        then in your controller: &lt;code&gt;name= params[:customer][:name]&lt;/code&gt; and
        &lt;code&gt;Customer.find_all_by_name(name)&lt;/code&gt; returns more than one entry you need
        to be a little more tricky to retrieve the actual record you wanted to
        select.&lt;/p&gt;
        
        &lt;p&gt;There are a couple of ways to do it. &lt;/p&gt;
        
        &lt;p&gt;One method is mentioned here: &lt;/p&gt;
        
        &lt;p&gt;&lt;a href=&quot;http://www.dalemartenson.com/blog/?p=24&quot;&gt;http://www.dalemartenson.com/blog/?p=24&lt;/a&gt;
        which hides the id field being returned.&lt;/p&gt;
        
        &lt;p&gt;Another method is here
        &lt;a href=&quot;http://ricardo.pacheco.name/blog/articles/2006/09&quot;&gt;http://ricardo.pacheco.name/blog/articles/2006/09&lt;/a&gt;
        which uses javascript to write the id into a hidden_field.&lt;/p&gt;
        
        &lt;p&gt;This &lt;a href=&quot;http://wiki.rubyonrails.com/rails/pages/How+to+use+text_field_with_auto_complete&quot;&gt;wiki entry&lt;/a&gt;
         (about half way down) suggests another way to do it, putting
        the id in the id tag of the &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; andf fetching it with javascript.&lt;/p&gt;
        
        &lt;p&gt;Another method I use is to put in the &lt;code&gt;text_field&lt;/code&gt; a string like
        &lt;code&gt;&quot;23,Blogs,Fred&quot;&lt;/code&gt;, this is the id of the customer record, and the
        last,first name. Then I do this in the controller method that receives
        the form data (eg def create) ...&lt;/p&gt;
        
        &lt;pre&gt;&lt;span class=&quot;ident&quot;&gt;namecsv&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:customer&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;ident&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;namecsv&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;('&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;')&lt;/span&gt;
        &lt;span class=&quot;ident&quot;&gt;customer&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
        &lt;/pre&gt;
        
        &lt;p&gt;Although in reality I usually write a setter in the Model to handle
        the csv so I can simply pass the entire params to the model ie
        &lt;code&gt;Customer.new(params)&lt;/code&gt;&lt;/p&gt;
        
        &lt;p&gt;I get the namecsv in the text box using this partial for the auto
        completer...&lt;/p&gt;
        
        &lt;pre&gt;&lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;ul&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;auto_complete&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;punct&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;customer&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;attribute&quot;&gt;@customers&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;-%&amp;gt;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;
           &amp;lt;li class=&amp;quot;big&amp;quot;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;gt;&lt;/span&gt;
             &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&amp;gt;&amp;lt;%=&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;h customer.fullname -%&amp;gt;&amp;lt;/div&amp;gt;
             &amp;lt;div class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&amp;gt;&amp;lt;%=&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;h &amp;quot;&lt;span class=&quot;expr&quot;&gt;#{customer.id}&lt;/span&gt;,&lt;span class=&quot;expr&quot;&gt;#{customer.lname}&lt;/span&gt;,&lt;span class=&quot;expr&quot;&gt;#{customer.fname}&lt;/span&gt;&amp;quot; -%&amp;gt;&amp;lt;/div&amp;gt;
             &amp;lt;div class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&amp;gt;&lt;/span&gt;
               &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;informal&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&amp;gt;&amp;lt;%=&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;h &amp;quot;&lt;span class=&quot;expr&quot;&gt;#{customer.email}&lt;/span&gt;&amp;quot; -%&amp;gt;&amp;lt;/span&amp;gt;
             &amp;lt;/div&amp;gt;
           &amp;lt;/li&amp;gt;
        &amp;lt;% end -%&amp;gt;
        &amp;lt;/ul&amp;gt;&lt;span class=&quot;normal&quot;&gt;
        &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
        
        &lt;p&gt;using this in the view...&lt;/p&gt;
        
        &lt;pre&gt;&lt;code&gt;&amp;lt;% text_field_with_auto_complete( :customer, :name, {}, {:select =&amp;gt; 'code', :skip_style =&amp;gt; true) %&amp;gt;
        &lt;/code&gt;&lt;/pre&gt;
        
        &lt;p&gt;Notice the &lt;code&gt;:select =&amp;gt; 'code'&lt;/code&gt;, this is critical as it tells it which part of the popup list to put 
        into the text_field.&lt;/p&gt;
        
        &lt;p&gt;This is a little ugly and error prone so you need some error checking etc.
        The other method looks nicer on the screen but is more work in the background.&lt;/p&gt;
        
        &lt;p&gt;&lt;a href=&quot;http://technorati.com/tag/text_field_with_auto_complete&quot; rel=&quot;tag&quot;&gt;&lt;/a&gt;&lt;/p&gt;
      </description>
      <author>Jim Morris</author>
      <pubDate>Mon, 23 Oct 2006 13:58:46 -0700</pubDate>
      <link>http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text_field_with_auto_complete</link>
      <guid isPermaLink='false'>urn:uuid:7411053b-dbe6-4421-b24d-5ea4e70ebb97</guid>
    </item>
    <item>
      <title>"Getting a record id from text_field_with_auto_complete" by Chris</title>
      <description>Very useful, thank you.</description>
      <pubDate>Sat, 07 Jul 2007 05:39:30 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/13#comment-51</link>
      <guid isPermaLink='false'>urn:uuid:1b130e3c-6782-4b16-86f0-5c26f0c2a369</guid>
    </item>
    <item>
      <title>"Getting a record id from text_field_with_auto_complete" by Jeremy Maziarz</title>
      <description>I forked DHH's auto_complete plugin to add the record id to the id tag of the &lt;li&gt; element. There is an example in the README of how to use the id using :after_update_element. Check it out on GitHub at http://github.com/jmaziarz/auto_complete</description>
      <pubDate>Thu, 31 Dec 2009 11:51:51 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/13#comment-297</link>
      <guid isPermaLink='false'>urn:uuid:9c64f55c-04c8-451a-91c7-19a9157aa2a1</guid>
    </item>
    <item>
      <title>"Getting a record id from text_field_with_auto_complete" by Richard</title>
      <description>awesome, thanks for that.</description>
      <pubDate>Thu, 08 Apr 2010 06:16:29 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/13#comment-337</link>
      <guid isPermaLink='false'>urn:uuid:9bc0d56f-0285-4bcb-9a88-841a1cdb07b6</guid>
    </item>
  </channel>
</rss>
