# Select\_tag help

**URL:** <https://www.ruby-forum.com/t/select-tag-help/163528>\
**Category:** Rails\
**Created:** [March 21, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/select-tag-help/163528 "2009-03-21T14:44:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [March 21, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/select-tag-help/163528/1 "2009-03-21T14:44:24Z")

</div>

I’m trying to create a select tag for a search form that defaults to  
–\>Fake when nothing has been selected. I have tried using the  
following:

\<%= select\_tag(:fake, options\_for\_select({"–\>Fake" =\> “”, “Yes” =\> 1,  
“No” =\> 0}, params[:fake].to\_i)) %\>

for some reason though, No is selected initially when no GET parameter  
is passed and the options are not in the correct order. they show up  
like the following:

No  
–\>Fake  
Yes

Can you someone help me out with this? thanks!

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [March 21, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/select-tag-help/163528/2 "2009-03-21T15:30:47Z")

</div>

On Mar 21, 1:44 pm, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

Hashes are not ordered in ruby 1.8 - if you want an ordering use an  
array (eg [[‘Yes’, 1], [‘No’,0]]).  
For an option to be preselected it’s value (the second bit) must be  
equal to the last argument to select\_tag (equal in the sense of ==, so  
in particular ‘1’ != 1)

In your case you are passing params[:fake].to\_i. If there are no  
parameters then params[:fake] is nil, and nil.to\_i == 0 so rails  
selects the option with value 0.

Fred

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [March 21, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/select-tag-help/163528/3 "2009-03-21T17:08:34Z")

</div>

Frederick C. wrote:

> On Mar 21, 1:44ï¿½pm, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 
> 
> Hashes are not ordered in ruby 1.8 - if you want an ordering use an  
> array (eg [[‘Yes’, 1], [‘No’,0]]).  
> For an option to be preselected it’s value (the second bit) must be  
> equal to the last argument to select\_tag (equal in the sense of ==, so  
> in particular ‘1’ != 1)
> 
> In your case you are passing params[:fake].to\_i. If there are no  
> parameters then params[:fake] is nil, and nil.to\_i == 0 so rails  
> selects the option with value 0.
> 
> Fred

thanks fred. changing to an array fixed the ordering problems.  
for the select statement, i changed the initial value for --\>Fake to -1

select\_tag(:fake,  
options\_for\_select([[“–\>Fake”,“-1”],[“Yes”,1],[“No”,0]],  
params[:fake].to\_i))

everything works great!
