Selected Value Not Persisting

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

Steve G. 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

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 P. [email protected]

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 P. [email protected]

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:

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 G. 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 P. [email protected]

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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs