I’m using the Rails select() form helper to generate a <select> structure. The following line works, but results in an unsorted list. How would I change the sort order to ascending alphabetical?
<%= select('category', 'name', Category.all.collect { |category| [ category.name ] }) %>
You should use the Rails 3 way:
This will generate the HTML options for each category like following:
Where the value is the Category id and not its name:
You will get the
params[:category_id]instead ofparams[:category]which would be a string containing the category name. Then to find a Category in the DB, it’s much better to use an id (because they are unique, not like names) and its faster!