Cookbook recipes eg - ordering categories in the recipe pull


#1

People,

I can order categories when looking at the categories list page but how
do I change the recipe page to list the categories in alpha order in the
recipe pull-down box?:

Category:
<% @categories.each do |category| %> > <%= category.name %> <% end %>

Thanks,

Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: removed_email_address@domain.invalid


#2

In your controller just modify the query that loads the @categories
variable to return the rows in the correct sort order.

Something like…

@categories = Categories.find(:all, :order=>“name”)


#3

Philip,

Have a look at
http://blog.teksol.info/articles/2006/01/03/belongs_to-user-interface-take-ii

It shows you

  • how to order, and also
  • how to validate for presence
  • how to highlight for validation error

Alain


#4

in recipe_controller.rb:

def show
@recipe = Recipe.find(params[:id])
@categories_for_select = Category.find(:all, :order => “name”).collect
{
|c| [c.name, c.id] }
end

in show.rhtml:

<%= select “recipe”, “category_id”, @categories_for_select %>

the option that = @recipe.category_id will automatically be selected.