Sort or Order a <Select> box


#1

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.

<% @peoplekinds.each do |peoplekind| %> > <%= peoplekind.name %> <% 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.


#2

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 http://api.rubyonrails.com/classes/ActionView/
Helpers/FormOptionsHelper.html#M000356.

-Ben


#3

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…


#4

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 …

end

that it needs to appear in.

Thanks again.