Sort or Order a <Select> box

Evening folks,

I’m new to RoR, and have found some very useful info on this site,

I have a select box that I need to sort.

<% @peoplekinds.each do |peoplekind| %> > <%= %> <% end %>

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.

In the controller for the List page I am able to use, :order_by =>
‘name’ , under the Def List.

When you retrieve @peoplekinds, either use sort_by or pass the :order
=> ‘name’ parameter when you call your model’s find method.

You can also use options_from_collection_for_select(@peoplekinds,
“id”, “name”, @people.peoplekind_id) to generate all the
tags for you; see


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]},

Hope this helps…

Thanks for the info, and thanks to those that emailed me with help.

The above two examples seem to be beyond me, but made for interesting
reading, I have homework to do.

The soloution I came up with was to include the following under the
controller for the page in question,

@peoplekind_pages, @peoplekinds = paginate(:peoplekinds, :order_by =>
‘name’, :per_page => 30)

This seems to work but I’m gussing it is not a very ellegant approach,
as I will have to repeat this under all the

def’s …


that it needs to appear in.

Thanks again.

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