# Selected Value Not Persisting

**URL:** <https://www.ruby-forum.com/t/selected-value-not-persisting/133478>\
**Category:** Rails\
**Created:** [March 31, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/selected-value-not-persisting/133478 "2008-03-31T23:31:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 31, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/selected-value-not-persisting/133478/1 "2008-03-31T23:31:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 1, 2008, 5:02am UTC](https://www.ruby-forum.com/t/selected-value-not-persisting/133478/2 "2008-04-01T05:02:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 1, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/selected-value-not-persisting/133478/3 "2008-04-01T17:00:36Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 2, 2008, 4:03am UTC](https://www.ruby-forum.com/t/selected-value-not-persisting/133478/4 "2008-04-02T04:03:36Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 3, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/selected-value-not-persisting/133478/5 "2008-04-03T16:52:21Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 4, 2008, 1:19am UTC](https://www.ruby-forum.com/t/selected-value-not-persisting/133478/6 "2008-04-04T01:19:20Z")

</div>

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/](http://sensei.zenunit.com/)
