Text_field_with_auto_complete

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!

well, i imagine the text field is part of a form which when submitted
calls a controller action. In this case you can get the value as part
of the standard params hash passed by rails, pretty much simliar to the
:id param that can be passed.

For ex

code in the view
text_field_with_auto_complete :customer, :name

can be retreived in the controller by using the following syntax
@cust = params[:customer]
@cust_name = @cust[“name”]

Regards,
Bharat
http://blog.publishedperspectives.com

Jacquie F. 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…

    <% for customer in @customers do -%>
  • <%=h customer.fullname -%>
    <%=h "#{customer.id},#{customer.lname},#{customer.fname}" -%>
    <%=h "#{customer.email}" -%>
  • <% end -%>

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.


Jim M., http://blog.wolfman.com

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

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
http://blog.wolfman.com/articles/2006/10/23/getting-a-record-id-from-text_field_with_auto_complete

Jim M. wrote:

There are a couple of ways to do it. The previous poster suggested one way which is to get it from

 <div class="code"><%=h "#{customer.id},#{customer.lname},#{customer.fname}" -%></div>

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.


Jim M., http://blog.wolfman.com

Jacquie F. wrote:

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.


Jim M., http://blog.wolfman.com

Thanks for your help,Jim.

Right now I have this table called places which has three columns
id,location,state and I autocomplete location on the webpage.

in the _form.rhtml file, I have

Travel Location: (the place that you have been)
<%= text_field_with_auto_complete :place, :location, {},{:select => 'location', :skip_style => true} %>

which invokes auto_complete_for…in the controller

content_controller.rb

def auto_complete_for_place_location
@places = Place.find(:all, :conditions => [ ‘LOWER(location) LIKE
?’,’%’ + params[:place][:location].downcase + ‘%’ ],:order => ‘location
ASC’,:limit => 8)
render :partial => ‘places’
end

and in views/admin/content folder, I have this partial _places.rhtml
which help to display the result on the screen.

somehow this is only the frontend work and has nothing to do with the
database.
now I am looking forward to add an entry to the database as soon as I
update the page.ie saving or submitting.

the question is while we type in word, the @places consists of a table
according to how many characters we typed in.(more characters,fewer
table entries) But after we complete the text field(chose one from the
auto-list),where in the code is storing our choice…

I will keep on looking at your answers. and hopefully will find a
solution soon.=)

Thanks again for your help!

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 “,”?

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.

PS.is your email address webmaster of that blog.

Thanks!

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 F. wrote:

‘location’, :skip_style => true} %>
render :partial => ‘places’
the question is while we type in word, the @places consists of a table
according to how many characters we typed in.(more characters,fewer
table entries) But after we complete the text field(chose one from the
auto-list),where in the code is storing our choice…

I will keep on looking at your answers. and hopefully will find a
solution soon.=)

Thanks again for your help!


Jim M., http://blog.wolfman.com

Answers in-line…

Jacquie F. 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)

    <% for customer in @customers do -%>
  • <%=h customer.fullname -%>
    <%=h "#{customer.id},#{customer.lname},#{customer.fname}" -%>
    <%=h "#{customer.email}" -%>
  • <% end -%>

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.

Thanks!


Jim M., http://blog.wolfman.com