| Article: |
Rolling with Ruby on Rails, Part 2 | |
| Subject: | "Showing recipes in a category" code | |
| Date: | 2005-03-08 08:36:56 | |
| From: | curth | |
|
Response to: "Showing recipes in a category" code
|
||
|
Even better, I could have used:
This is an example of dynamic finders, where "find_by_" can be appended with a column name. |
||
Showing messages 1 through 7 of 7.
-
"Showing recipes in a category" code
2005-03-10 02:21:34 aeden [View]
-
"Showing recipes in a category" code
2005-03-10 03:37:24 Curt Hibbs |
[View]
Oops, you are correct, I should have written:
def list
@category_id = @params['category']
@recipes=Recipe.find_by_category_id(@category_id)
end
-
"Showing recipes in a category" code
2005-03-11 15:46:16 automat_svet [View]
I get this error: undefined method `each' for nil:NilClass
sure I'm doing something wrong in list.rhtml :-(
any suggestion?
Thanks -
"Showing recipes in a category" code
2005-04-05 12:45:39 co_banzai [View]
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 thecategoryparameter, 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 usefind_all_by_category_idinstead offind_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 -
"Showing recipes in a category" code
2005-04-22 03:38:42 xeal [View]
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)
end
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>
-
"Showing recipes in a category" code
2005-03-11 16:57:37 Curt Hibbs |
[View]
First of all, let me say that what I answered above about using @category_id is incorrect, the article is correct as written (I must have been on drugs when I wrote that :-).
In you case, I'm assuming that you cut'n'paste the code for list.rhtml, so the line on which you are getting the error contains "@recipes.each", which is going to iterate through each member of the collection contained in @recipes.
The error says the @recipes contains the value "nil" (the Ruby equivalent of Java's null), and that the object "nil" does not have a method named "each".
That means that something is wrong in the controller where @recipes is being assigned its value. Make sure you list method looks like this:
def list
@category = @params['category']
@recipes = Recipe.find_all
end
If it already does, then that means that find_all is failing to find any recipes in the database (or failing to find the database).
Did you go through part 1 successfully? Does your database contain some recipes?
You could try starting off the my zip file for your code and use my sql file to initialize your database, and then start working through part 2.
-
"Showing recipes in a category" code
2005-03-14 07:30:33 automat_svet [View]
Curth,
first of all thank you very much for the fast reply and really usefull article.
I had no problem following your tutorial, the error message came out when I tried to implement the 'find_by' as suggested in the previous comments.
I'm really new in all this and I'm just tring to learn different ways to do the same job.
Cheers
Automat_svet



@recipes = Recipe.find_all ["category_id = ?", @category]I did have to make some changes to the HTML. First of all the category passed as the argument from the list page should be the category ID and not the name. Second of all you will need to remove the if statement in list.rhtml. I spent 10 minutes trying to figure out why it wasn't showing the filtered recipe list before I realized that this was the problem.