text_field_with_auto_complete

Jacquie Fan wrote:

Hi,all

I've used text_field_with_auto_complete for a while but still haven't figured out how to store the value that I selected from the suggested options. any hints?

Thanks!

There are a couple of ways to do it. The previous poster suggested one way which is to get it from the params like you would a normal text_field, however this only works if the value you select and display in the text_field part of the auto_completer is unique in the database.. e.g. if text_field_with_auto_complete :customer, :name then if name= params[:customer][:name] is not unique, ie Customer.find_all_by_name(name) returns more than one entry you need to be a little more tricky.

One method is mentioned here: http://www.dalemartenson.com/blog/?p=24 and I use this sometimes.

Another method I use is to put in the text_field a string like "23,Blogs,Fred", this is the id of the customer record, and the last,first name. Then I do this in the controller...

namecsv= params[:customer][:name] id,last,first= namecsv.split(',') customer= Customer.find(id)

I get the namecsv in the text box using this partial for the auto completer...

<ul class="auto_complete"> <% for customer in @customers do -%>    <li class="big">      <div class="name"><%=h customer.fullname -%></div>      <div class="code"><%=h "#{customer.id},#{customer.lname},#{customer.fname}" -%></div>      <div class="email">        <span class="informal"><%=h "#{customer.email}" -%></span>      </div>    </li> <% end -%> </ul>

using this in the view...

text_field_with_auto_complete( :customer, :name, {}, {:select => 'code', :skip_style => true) %>

Notice the :select => 'code', this is critical as it tells it which part of the popup list to put into the text_field.

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.

So use whichever method best suits your application.

Seeing as this question seem to come up a lot, and I certainly had to google around to get the answer(s) I added a blog entry on it Wolfmans Howlings

Jim Morris wrote:

Jacquie Fan wrote:

Thanks for that, Jim?

another dummy question. do you place this code

namecsv= params[:customer][:name] id,last,first= namecsv.split(',') customer= Customer.find(id)

into your auto_complete_for...method? since I put it in another method and it didnt seem to be able to find the right :customer and :name so namecsv is and both of id, last and first are nil.

THanks

Thats a good question, I actually put that in the Customer model under

def name=(namecsv) ... end

I override the name= in this case to expect the csv data, as I usually pass the entire params array into the Customer.new(params), for instance.

However the code I wrote about above should go in the controller method that handles the submit from the form, ie def submit or def create etc. because that is where the params are sent to.

If you can give me more specific examples of how you are using it I can be more specific.

Ahh, ok I see what you are getting at now :slight_smile:

text_field_with_auto_complete does not submit the place when you select it from the drop down list, all that does is put the result (in this case whatever is in the class=location in your _places.rhtml) into the text box named 'place[location]'.

When you submit the form (I presume this is in a form) then the action for the form will get in its params a hash {"place" => {"location" => "Paris"} }

so for instance if your text_field... is in a form...

<% form_for :place, @places, :url => { :action => 'create' } do |f| %> ... <%= text_field_with_auto_complete :place, :location, {},{:select => 'location', :skip_style => true} %> ...   <%= submit_tag "Submit" %> <% end %>

then in your controller, when you click the submit button...

def create

  selected_place= params[:place][:location]   puts "your selected place is #{selected_place}"

end

Thats where the selected place will be handed back to you for saving in the database.

If you wanted to have the selected place immediately written to the database when it is selected, you would need to use something like the in_place_editor_field AJAX call, but that is just an edit box you would have to write your own version for a drop down box, I think I saw an example somewhere, email me if you actually want to do it that way.. (Although I think you probably don't :slight_smile:

I hope that answers your question, feel free to email for more help, the email is in the blog address I gave you last time.

Jacquie Fan wrote:

Answers in-line...

Jacquie Fan wrote:

I see...back to the question on your blog, according to the following method.

namecsv= params[:customer][:name] id,last,first= namecsv.split(',') customer= Customer.find(id)

I assume namecsv has three columns? id,last,first.and delimited by ","?

It is whatever you send, in my case I send the three items, but only the first is useful as it is the id of the record I want to update or link to.

I tried to put breakpoint in my code,and then I print out the value

params[:place]=>{"location"=>"Adelaide"}

params[:place][:location]=>"Adelaide"

in my place table,I have three columns, id,location and state. I wonder if I could return the id corresponding to "Adelaide" in the place table.

So there was a piece missing from your last post, what is the code you use for generating the auto_text pop_up list? In my case I use this... (from my blog)

<ul class="auto_complete"> <% for customer in @customers do -%>     <li class="big">       <div class="name"><%=h customer.fullname -%></div>       <div class="code"><%=h "#{customer.id},#{customer.lname},#{customer.fname}" -%></div>       <div class="email">         <span class="informal"><%=h "#{customer.email}" -%></span>       </div>     </li> <% end -%> </ul>

Note the code class is a string with three items separated by , and that is what goes into the text field and gets posted to the controller when I click create. and that is what I call textcsv in the controller.

PS.is your email address webmaster of that blog.

Yes just click the send me email link on the blog.