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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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. -- Jim Morris, http://blog.wolfman.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Morris wrote:> 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. > > >-- Jim Morris, http://blog.wolfman.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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. -- Jim Morris, http://blog.wolfman.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 <p> <label for="article_place">Travel Location:</label> (the place that you have been) <br> <%= text_field_with_auto_complete :place, :location, {},{:select => ''location'', :skip_style => true} %> </p> 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Ahh, ok I see what you are getting at now :) 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 :) 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:> 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 > <p> > <label for="article_place">Travel Location:</label> > (the place that you have been) <br> > <%= text_field_with_auto_complete :place, :location, {},{:select => > ''location'', :skip_style => true} %> > </p> > > 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! >-- Jim Morris, http://blog.wolfman.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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.> > Thanks! >-- Jim Morris, http://blog.wolfman.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Hi I have been trying out text_field_with_auto_complete and initially defined the following in my view: <%= text_field_with_auto_complete(:project, :project_code, :size => ''10'') %> with the corresponding code in my controller: auto_complete_for :project, :project_code But I wanted the name of the text field to be project_code instead of project[project_code] so I added a :name option <%= text_field_with_auto_complete(:project, :project_code, :name => ''project_code'', :size => ''10'') %> But then this seems to stop the auto_complete working and I cannot quite see why? I defined my own method auto_complete_for_project_project_code using the source from auto_complete_for and then it works but still do not see what I have really done any different. def auto_complete_for_project_project_code method = :project_code object = :project options = {} find_options = {:conditions => [ "LOWER(#{method}) LIKE ?", ''%'' + params[method].downcase + ''%'' ], :order => "#{method} ASC", :limit => 10 }.merge!(options) @items = object.to_s.camelize.constantize.find(:all, find_options) render :inline => "<%= auto_complete_result @items, ''#{method}'' %>" end I must be doing something stupid ;-) Does anyone have any suggestions? Thanks Shane -- Shane Mingins --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---