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

advertisement

AddThis Social Bookmark Button
Article:
  Rolling with Ruby on Rails, Part 2
Subject:   "Showing recipes in a category" code
Date:   2005-04-05 12:45:39
From:   co_banzai
Response to: "Showing recipes in a category" code

Yeah, I got the same error. I found that I had to make a couple changes to get this working.


1) On the list.rhtml page, there is a line that reads:


:category => "#{recipe.category.name}" %>


This results in it passing the categoty name in the category parameter, which is wrong; it needs to pass the category_id To make things clearer, I also altered the name of the parameter to reflect this change. After doing so, the above line of code was changed to:


:category_id => "#{recipe.category.id}" %>


2) I had to update the list method of the recipe controller class in two ways. First, I had to put in a conditional clause so that when the category_id is not defined, it returns all of the data (I'm new to RoR, so there may be a better way to do this). Second, I had to use find_all_by_category_id instead of find_by_category_id. According to the docs, I shouldn't need to do this but I found that without this I got an error (I'm on Rails 0.11). When it was all said and done, my list method looked like so:

def list
@category_id = @params['category_id']
if @category_id
@recipes = Recipe.find_all_by_category_id(@category_id)
else
@recipes = Recipe.find_all
end
end


Hope that helps


1 to 1 of 1
1 to 1 of 1