"You have a nil object" problem driving me crazy!

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

<%= side_image.filename.to_s  %>

with

<%= side_image.filename.to_s  %>

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:

<% @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 %>
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

Hi,

I’ve made a mistake in my description above. Here’s the corrected
version:

If I replace the

<%= side_image.filename.to_s  %>

with

<%= debug(side_image.filename)  %>

I’m desperate! Thank you for your help!

Kind regards,
John

On 30 Nov 2008, at 19:58, John D. wrote:

<%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

The obvious answer would be that you have a dilemma side with no side
images.

Fred

Frederick C. wrote:

The obvious answer would be that you have a dilemma side with no side
images.

Fred

Hi Fred,

Thank you, but as you can see above, the output of the debug command
above, side_image does contain a Side Image:

— !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: {}

I’m just not able to access the attributes!

Cheers,
John

On 30 Nov 2008, at 20:07, John D. wrote:

Thank you, but as you can see above, the output of the debug command
above, side_image does contain a Side Image:

Are you sure that’s not displaying the side image from a previous
iteration through the loop ?

How about you change your code to

<% @dilemmas.each do |dilemma| %>
<% dilemma.dilemma_sides.each do |dilemma_side| %>

  <% side_image = dilemma_side.side_images.first %>
  <% if !side_image %>
    Side <%= dilemma_side.id %> has no images
  <% end %>
<%end%>

<% end %>

and see what it outputs.

Fred

Damn, again made a mistake in my description:

<%= side_image.filename.to_s  %>

with

<%= debug(side_image)  %>

That’s it guys, that’s the correct version. Sorry for the confusion!

How about you change your code to

<% @dilemmas.each do |dilemma| %>
<% dilemma.dilemma_sides.each do |dilemma_side| %>

  <% side_image = dilemma_side.side_images.first %>
  <% if !side_image %>
    Side <%= dilemma_side.id %> has no images
  <% end %>
<%end%>

<% end %>

and see what it outputs.

Fred

Hey,

I’ve tried it, and as expected, it does not go into the loop. I am sure
that it is finding the Side Image belonging to the Dilemma Side. For
some strange reason, when I loop through the array which contains the
Side Images:

<% dilemma_side.side_images.each do |side_image|%>
<%= side_image.filename.to_s %>
<%end>

Then Rails allows me to access the attribute directly. I’m certainly
doing something wrong with instance variables assignments/declarations.

Hi –

On Sun, 30 Nov 2008, John D. wrote:

  <% end %>

that it is finding the Side Image belonging to the Dilemma Side. For
some strange reason, when I loop through the array which contains the
Side Images:

<% dilemma_side.side_images.each do |side_image|%>
<%= side_image.filename.to_s %>
<%end>

Then Rails allows me to access the attribute directly. I’m certainly
doing something wrong with instance variables assignments/declarations.

Is the code you’re showing us cut-and-pasted from your views? It looks
like it might be a typo (like @side_image for side_image), so to rule
that out I wanted to be sure this is the actual template code.

David


Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS (Jan 12-15), Fort Lauderdale, FL
See http://www.rubypal.com for details
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

Is the code you’re showing us cut-and-pasted from your views? It looks
like it might be a typo (like @side_image for side_image), so to rule
that out I wanted to be sure this is the actual template code.

Hi David,

Yes, it is an actual copy an paste.

Thanks!
John

Could you maybe copy and paste it into pastie so that it is easier for
everyone to look at?

Guys,

The issue is solved. I dropped and repopulated my database, and it now
works. I guess Fred was right after all, I was probably having a Side
without an Image in one of my loops.

Thanks for your time! I appreciate it.

Kind regards,
John

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs