On 7/8/06, Nick <nickperez@gmail.com> wrote:> I''m trying to teach myself Ruby on Rails, and doing the infamous
> Cookbook as a starting point. I''m all set until the point when I
go to
> create of sumbit my recipe and I get this error:
>
> NoMethodError in Recipe#index
>
> You have a nil object when you didn''t expect it!
> The error occured while evaluating nil.name
>
> 15: <% @recipes.each do |recipe| %>
> 16: <tr>
> 17: <td><%= link_to recipe.title, :action =>
"show", :id => recipe.id
> %></td>
> 18: <td><%= recipe.category.name %></td>
> 19: <td><%= recipe.date %></td>
> 20: </tr>
> 21: <% end %>
>
>
> when I look in my MySQL database, the recipe is indeed in the db, but I
> still get that error. Any help would be greatly appreciated.
Looks to be complaining about the category being nil, do you have a
category assigned and saved for the recipe you''re trying to load?
When dealing with children''s attributes I find that I almost always
tag a conditional onto the end of the line, you just never know when
someone is going to do something stupid to the database (I work with a
bunch of people that know databases but don''t know Rails).
Something like:
<%= recipe.category.name if recipe.category %>
should help out with the NoMethodError until you can figure out why
exactly there isn''t a method in the first place :)
Ta,
Chuck Vose