Hi!
I've got the following problem:
FYI:
- @dilemmas is populated in the controller via: @dilemmas =
Dilemma.find(:all)
- the Dilemma model has_many :dilemma_sides
- the DilemmaSide model belongs_to :dilemma
- the DilemmaSide model has_many :side_images
- the SideImage model belongs_to :dilemma_side
- side_images table has following attributes
# Table name: side_images
#
# id :integer(4) not null, primary key
# dilemma_side_id :integer(4) not null
# parent_id :integer(4)
# size :integer(4)
# width :integer(4)
# height :integer(4)
# content_type :string(255)
# filename :string(255)
# thumbnail :string(255)
# created_at :datetime
# updated_at :datetime
#
Here's the code from my view.
[code=]<% @dilemmas.each do |dilemma| %>
<% dilemma.dilemma_sides.each do |dilemma_side| %>
<% side_image = dilemma_side.side_images.first %>
<%= side_image.filename.to_s %>
<%end%>
<% end %>[/code]
When I execute the code, I get the following error:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.filename
If I replace the
[code=]<%= side_image.filename.to_s %>[/code]
with
[code=]<%= side_image.filename.to_s %>[/code]
I can confirm that the side_image has is correctly pointing to a
SideImage object:
--- !ruby/object:SideImage
attributes:
content_type: image/jpeg
size: "44882"
thumbnail:
updated_at: 2008-11-28 19:06:35
id: "1"
dilemma_side_id: "1"
height: "256"
filename: IMG_0026.JPG
parent_id:
width: "341"
created_at: 2008-11-28 19:06:35
attributes_cache: {}
If on the other hand, I change the code in the following way:
[code=]<% @dilemmas.each do |dilemma| %>
<% dilemma.dilemma_sides.each do |dilemma_side| %>
<% dilemma_side.side_images.each do |side_image|%>
<%= side_image.filename.to_s %>
<%end>
<%end%>
<% end %>[/code]
Then it works! But the problem with this solution is that I am looping
through all side_images associated with the dilemma_side. I only want
to display the first image.
I have no idea why I am getting this nil object error, and I've tried
everything I could think of. I am relatively new to Ruby on Rails, so
please help!
Thanks!
John
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---