Forum: Ruby on Rails Cookbook recipes eg - ordering categories in the recipe pull

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
55f28e9c77b35b1539af6be60986b0e4?d=identicon&s=25 Philip Rhoades (Guest)
on 2006-01-06 06:58
(Received via mailing list)
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?:


>   <p><b>Category:</b><br>
>   <select name="recipe[category_id]">
>    <% @categories.each do |category| %>
>        <option value="<%= category.id %>"
>          <%= ' selected' if category.id == @recipe.category_id %>>
>          <%= category.name %>
>        </option>
>    <% end %>
>   </select></p>


Thanks,

Phil.
--
Philip Rhoades

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:  phil@pricom.com.au
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-06 07:26
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")
B45eab4f18aa1bb2a44d6e657531a642?d=identicon&s=25 Alain Ravet (aravet)
on 2006-01-06 13:53
Philip,

Have a look at
    http://blog.teksol.info/articles/2006/01/03/belong...

It shows you
   - how to order, and also
   - how to validate for presence
   - how to highlight for validation error

Alain
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-01-06 14:50
(Received via mailing list)
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.
This topic is locked and can not be replied to.