hi, i''m trying to add a select box with the select method, but rails orders it according to the name, while i needed ordered according to the value, or just to work in the order i put it in. this is my array: @price_range = {"$100-$200" => "0", "$200-$300" => "1", "$300-$350" => "2", "$350-$500" => "3"} and this is my view: <% fields_for(@description) do |d| -%> <%= d.select( :price_range, @price_range ) %> <%end$> i can''t figure out how to do this, it subtracts one number from the other number (100 from 200) and then orders from lowest to highest, which is not what I want. any help is greatly appreciated, 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 Sep 14, 8:38 pm, Daniel Fac <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> hi, > i''m trying to add a select box with the select method, but rails orders > it according to the name, while i needed ordered according to the value, > or just to work in the order i put it in. > this is my array: > @price_range = {"$100-$200" => "0", "$200-$300" => "1", "$300-$350" => > "2", "$350-$500" => "3"} > and this is my view: > <% fields_for(@description) do |d| -%> > <%= d.select( :price_range, @price_range ) %> > <%end$> > i can''t figure out how to do this, it subtracts one number from the > other number (100 from 200) and then orders from lowest to highest, > which is not what I want. > any help is greatly appreciated, > thanks > -- > Posted viahttp://www.ruby-forum.com/.That''s no array. It''s a hash, and hashes are unordered - or at the very least you can never rely on its ordering. Try this instead: <%= d.select(:price_range, @price_range.sort) %> Hope that works. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
thanks, that works great. is there a way to do this without having to sort? just having them in the order i placed them? Erol Fornoles wrote:> On Sep 14, 8:38�pm, Daniel Fac <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> <%end$> >> i can''t figure out how to do this, it subtracts one number from the >> other number (100 from 200) and then orders from lowest to highest, >> which is not what I want. >> any help is greatly appreciated, >> thanks >> -- >> Posted viahttp://www.ruby-forum.com/. > > That''s no array. It''s a hash, and hashes are unordered - or at the > very least you can never rely on its ordering. > > Try this instead: > > <%= d.select(:price_range, @price_range.sort) %> > > Hope that works.-- 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 -~----------~----~----~----~------~----~------~--~---
On 14 Sep 2008, at 14:10, Daniel Fac wrote:> > thanks, > that works great. > is there a way to do this without having to sort? just having them in > the order i placed them?Use an array instead of a hash (which is what sort does - turns the hash into an array of [key, value] arrays. Fred> > Erol Fornoles wrote: >> On Sep 14, 8:38�pm, Daniel Fac <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >>> <%end$> >>> i can''t figure out how to do this, it subtracts one number from the >>> other number (100 from 200) and then orders from lowest to highest, >>> which is not what I want. >>> any help is greatly appreciated, >>> thanks >>> -- >>> Posted viahttp://www.ruby-forum.com/. >> >> That''s no array. It''s a hash, and hashes are unordered - or at the >> very least you can never rely on its ordering. >> >> Try this instead: >> >> <%= d.select(:price_range, @price_range.sort) %> >> >> Hope that works. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
On Sep 14, 9:15 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 14 Sep 2008, at 14:10, Daniel Fac wrote: > > thanks, > > that works great. > > is there a way to do this without having to sort? just having them in > > the order i placed them? > > Use an array instead of a hash (which is what sort does - turns the > hash into an array of [key, value] arrays. > > FredWhich means you''ll need to change your @price_range declaration to an array of pairs: @price_range = [ ["$100-$200", 0], ["$200-$300", 1], ["$300-$350", 2], ["$350-$500", 3] ] --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---