[Rails] Sort or Order a <Select> box.

Steve Ross sross at calicowebdesigns.com
Sun Jan 1 00:53:12 GMT 2006


I've found this kind of construct works (where arg might be your AR).

options_for_select(arg.sort{|k,v| k[1] <=> v[1]}, selected)

So, perhaps you'd be using:

options_for_select(people.sort{|k,v| k[1] <=> v[1]}, @people.peoplekind_id)

Hope this helps...


On 12/31/05 1:00 PM, "Andy Park" <celtichuddle at mac.com> wrote:

> Evening folks,
> 
> I'm new to RoR, and have found some very useful info on this site,
> thanks.
> 
> I have a select box that I need to sort.
> 
> <select name="people[peoplekind_id]">
> <% @peoplekinds.each do |peoplekind| %>
> <option value="<%= peoplekind.id %>"
> <%= ' selected' if peoplekind.id == @people.peoplekind_id %>>
> <%= peoplekind.name %>
> </option>
> <% end %>
> </select></p>
> <input type="submit" value="Update" />
> 
> In the controller for the List page I am able to use,  :order_by =>
> 'name' , under the Def List.
> 
>  I have tried this in <% @peoplekinds.each do |peoplekind| %> above,
> but this does not work. How far off track am I?
> 
> Kindest regards.




More information about the Rails mailing list