Cookbook recipes eg - ordering categories in the recipe pull


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?:

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



Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: [email protected]

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”)


Have a look at

It shows you

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


in recipe_controller.rb:

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

in show.rhtml:

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

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

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