Hello again, Ok this is really starting to drive my nuts now so I was hoping someone could give me a hand. I have a restaurant search form that allows people to search out restaurants. I have a drop down that allows people to narrow the search to a specific cuisine type. When the page first loads I need a "Pick A Cuisine" prompt at the top of the list. The problem is that if the user selects a cuisine type then submits the form the selected value always goes back to the prompt. Can anyone please help show me how to keep the selected value? Here is what I have so far: collection_select(:restaurant, :cuisine_id ,Cuisine.find(:all), :id, :name, :prompt => "Pick A Cuisine") %> I also tried this: <%= select_tag(''cuisineid'' , options_for_select(Cuisine.find(:all).collect {|p| [ p.name, p.id ] }, params[:cuisineid])) %> Thanks, Steve -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Steve Glaz wrote:> selected value always goes back to the prompt. Can anyone please help > show me how to keep the selected value? Here is what I have so far: > > collection_select(:restaurant, :cuisine_id ,Cuisine.find(:all), :id, > :name, :prompt => "Pick A Cuisine") %> >> Thanks, > SteveSteve, The reason this is not happening is that there isn''t anything automagical in Rails to do this for you. What you should do is save the selection in a session variable when they submit the form, and then use that session variable to set the current setting of the selector in your view. (and condition the prompt on whether or not the session variable has been set up yet). hth, jp -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Another option would be to put the cuisine id in the route (routes.rb). I''m dealing with this in another app that has a similar approach. You could also set a hidden field in a form if the cuisine has been selected... Anyway, there are different ways to do this, so explore your options and take your pick! -Kyle On Mar 31, 10:02 pm, Jeff Pritchard <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Steve Glaz wrote: > > selected value always goes back to the prompt. Can anyone please help > > show me how to keep the selected value? Here is what I have so far: > > > collection_select(:restaurant, :cuisine_id ,Cuisine.find(:all), :id, > > :name, :prompt => "Pick A Cuisine") %> > > > Thanks, > > Steve > > Steve, > The reason this is not happening is that there isn''t anything > automagical in Rails to do this for you. What you should do is save the > selection in a session variable when they submit the form, and then use > that session variable to set the current setting of the selector in your > view. (and condition the prompt on whether or not the session variable > has been set up yet). > > hth, > jp > -- > Posted viahttp://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?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the tips Kyle. I was trying to avoid using a session variable but I will try playing around with hidden fields and see what happens! steve Kyle wrote:> Another option would be to put the cuisine id in the route > (routes.rb). I''m dealing with this in another app that has a similar > approach. You could also set a hidden field in a form if the cuisine > has been selected... Anyway, there are different ways to do this, so > explore your options and take your pick! > > -Kyle > > On Mar 31, 10:02 pm, Jeff Pritchard <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Hello, Ok I came up with an acceptable working solution. It may not be the best one but I will post it anyway for all of you who are experiencing the same problem. In My View I kept the same code for the select: <% collection_select(:restaurant, :cuisine_id ,Cuisine.find(:all), :id, :name, :prompt => "Don''t Care" ) %> However I added the following Java script at the top of the file: <script type="text/javascript"> document.observe(''dom:loaded'',function() { <% if !@cuisine_id.blank? %> document.getElementById("restaurant_cuisine_id").value = <%= @cuisine_id %> ; <% end %> }); </script> And of course in my Controller I set the @cuisine_id variable: @cuisine_id = params[:restaurant][:cuisine_id] If anyone has any comments/better solutions I would love to hear it. At least I managed to avoid using the session variable and hidden input elements. steve Steve Glaz wrote:> Thanks for the tips Kyle. I was trying to avoid using a session variable > but I will try playing around with hidden fields and see what happens! > > steve > > > > Kyle wrote: >> Another option would be to put the cuisine id in the route >> (routes.rb). I''m dealing with this in another app that has a similar >> approach. You could also set a hidden field in a form if the cuisine >> has been selected... Anyway, there are different ways to do this, so >> explore your options and take your pick! >> >> -Kyle >> >> On Mar 31, 10:02 pm, Jeff Pritchard <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- 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?hl=en -~----------~----~----~----~------~----~------~--~---
You could just pass the param(s) on with the form, and put a hidden_field_tag in the next form. Julian. Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3 OUT APRIL 6 http://sensei.zenunit.com/ On 04/04/2008, at 1:52 AM, Steve Glaz wrote:> > Hello, > > Ok I came up with an acceptable working solution. It may not be the > best > one but I will post it anyway for all of you who are experiencing the > same problem. > > In My View I kept the same code for the select: > > <% > collection_select(:restaurant, :cuisine_id ,Cuisine.find(:all), :id, > :name, :prompt => "Don''t Care" ) %> > > However I added the following Java script at the top of the file: > > <script type="text/javascript"> > document.observe(''dom:loaded'',function() > { > > <% if !@cuisine_id.blank? %> > document.getElementById("restaurant_cuisine_id").value = <%> @cuisine_id %> ; > <% end %> > > }); > </script> > > And of course in my Controller I set the @cuisine_id variable: > > @cuisine_id = params[:restaurant][:cuisine_id] > > If anyone has any comments/better solutions I would love to hear it. > At > least I managed to avoid using the session variable and hidden input > elements. > > steve > > > > > > > > > > > > Steve Glaz wrote: >> Thanks for the tips Kyle. I was trying to avoid using a session >> variable >> but I will try playing around with hidden fields and see what >> happens! >> >> steve >> >> >> >> Kyle wrote: >>> Another option would be to put the cuisine id in the route >>> (routes.rb). I''m dealing with this in another app that has a >>> similar >>> approach. You could also set a hidden field in a form if the >>> cuisine >>> has been selected... Anyway, there are different ways to do this, >>> so >>> explore your options and take your pick! >>> >>> -Kyle >>> >>> On Mar 31, 10:02 pm, Jeff Pritchard <rails-mailing-l...@andreas- >>> s.net> > > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---