ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button
  Rolling with Ruby on Rails, Part 2
Subject:   "Showing recipes in a category" code
Date:   2005-04-22 03:38:42
From:   xeal
Response to: "Showing recipes in a category" code

I also think that using the recipe.category.id field is better - after all the recipe.category.name might not be unique.

And the code simplifies, so you can write the whole RecipeController.list method as:

def list
@recipes = Recipe.find_all(@params["category_id"] ? ("category_id = " + @params["category_id"]) : nil)

The code above passes to Recipe.find_all a category_id, if @params["category_id"] is not empty or nil otherwise.

Of course, the statement from list.rhtml that renders the category needs to be changed as:

<td><%= link_to recipe.category.name, :action => "list", :category_id => recipe.category.id %></td>