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! -- 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 -~----------~----~----~----~------~----~------~--~---
On Mar 21, 1:44 pm, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: >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> No > -->Fake > Yes > > Can you someone help me out with this? thanks! > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Mar 21, 1:44�pm, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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. > > Fredthanks 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! -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---