<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <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>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A programmers Blog about Ruby, Rails and a few other issues</description>
    <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="http://www.dalemartenson.com/blog/?p=24"&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="http://ricardo.pacheco.name/blog/articles/2006/09"&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="http://wiki.rubyonrails.com/rails/pages/How+to+use+text_field_with_auto_complete"&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;"23,Blogs,Fred"&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;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;namecsv&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;params&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:customer&lt;/span&gt;&lt;span class="punct"&gt;][&lt;/span&gt;&lt;span class="symbol"&gt;:name&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;
&lt;span class="ident"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="ident"&gt;last&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="ident"&gt;first&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;namecsv&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;split&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;,&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
&lt;span class="ident"&gt;customer&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Customer&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;ul&lt;/span&gt; &lt;span class="keyword"&gt;class&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;auto_complete&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="keyword"&gt;for&lt;/span&gt; &lt;span class="ident"&gt;customer&lt;/span&gt; &lt;span class="keyword"&gt;in&lt;/span&gt; &lt;span class="attribute"&gt;@customers&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;-%&amp;gt;&lt;/span&gt;&lt;span class="string"&gt;
   &amp;lt;li class=&amp;quot;big&amp;quot;&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;div&lt;/span&gt; &lt;span class="keyword"&gt;class&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&amp;lt;%=&lt;/span&gt;&lt;span class="string"&gt;h customer.fullname -%&amp;gt;&amp;lt;/div&amp;gt;
     &amp;lt;div class&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&amp;lt;%=&lt;/span&gt;&lt;span class="string"&gt;h &amp;quot;&lt;span class="expr"&gt;#{customer.id}&lt;/span&gt;,&lt;span class="expr"&gt;#{customer.lname}&lt;/span&gt;,&lt;span class="expr"&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="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;email&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
       &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;span&lt;/span&gt; &lt;span class="keyword"&gt;class&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;informal&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&amp;lt;%=&lt;/span&gt;&lt;span class="string"&gt;h &amp;quot;&lt;span class="expr"&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&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&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="http://technorati.com/tag/text_field_with_auto_complete" rel="tag"&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 23 Oct 2006 13:58:46 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:7411053b-dbe6-4421-b24d-5ea4e70ebb97</guid>
      <author>Jim Morris</author>
      <link>http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text_field_with_auto_complete</link>
      <category>Rails</category>
      <category>rails</category>
      <category>text_field_with_auto_complete</category>
      <trackback:ping>http://blog.wolfman.com/articles/trackback/63</trackback:ping>
    </item>
    <item>
      <title>"Getting a record id from text_field_with_auto_complete" by Chris</title>
      <description>&lt;p&gt;Very useful, thank you. &lt;/p&gt;</description>
      <pubDate>Sat, 07 Jul 2007 05:39:30 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:1b130e3c-6782-4b16-86f0-5c26f0c2a369</guid>
      <link>http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text_field_with_auto_complete#comment-82</link>
    </item>
  </channel>
</rss>
