Select_tag help

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!

On Mar 21, 1:44 pm, Scott K. [email protected]
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

Frederick C. wrote:

On Mar 21, 1:44�pm, Scott K. [email protected]
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!

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