I have this hash constant:
EMPLOYEE_NUM_OPTIONS = {
'Please, select' => '',
'10-50' => '10-50',
'51-100' => '51-100',
'101-500' => '101-500',
'501-1999' => '501-1999',
'+2000' => '2000',
}
which I want to use in Rails form as options exactly in the order it is written. Using
<%= pf.select(:employee_num, GroupProfile::EMPLOYEE_NUM_OPTIONS.sort)%>
does not give the expected result.
Thank you for your help.
When you call
sorton the Hash, you will effectively convert the Hash to an array of key/value arrays and then sort those usingArray#<=>which will wreck your order. Whatever you hand topf.selectshould end up going throughoptions_for_select:Emphasis mine. So you should be able to say this if you’re using Ruby 1.9:
If you’re in 1.8 then you can change
EMPLOYEE_NUM_OPTIONSto an array-of-arrays:to get the order right and hand that to
pf.select. If you also need a Hash form then:should do the trick.
Again, if you’re using 1.9 then you’ll have ordered Hashes already so you don’t need all this extra work.