Forum: Ruby on Rails "You have a nil object" problem driving me crazy!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John D. (Guest)
on 2008-11-30 21:58
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
John D. (Guest)
on 2008-11-30 22:03
Hi,

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

> If I replace the

> [code=]<%= side_image.filename.to_s  %>[/code]

> with

> [code=]<%= debug(side_image.filename)  %>[/code]

I'm desperate!  Thank you for your help!

Kind regards,
John
Frederick C. (Guest)
on 2008-11-30 22:03
(Received via mailing list)
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
John D. (Guest)
on 2008-11-30 22:07
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
Frederick C. (Guest)
on 2008-11-30 22:13
(Received via mailing list)
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
John D. (Guest)
on 2008-11-30 22:14
Damn, again made a mistake in my description:

>> [code=]<%= side_image.filename.to_s  %>[/code]
>
>> with
>
>> [code=]<%= debug(side_image)  %>[/code]
>


That's it guys, that's the correct version.  Sorry for the confusion!
John D. (Guest)
on 2008-11-30 22:22
> 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.
David A. Black (Guest)
on 2008-11-30 22:28
(Received via mailing list)
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)
John D. (Guest)
on 2008-11-30 22:49
> 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
Bobnation (Guest)
on 2008-11-30 23:18
(Received via mailing list)
Could you maybe copy and paste it into pastie so that it is easier for
everyone to look at?
John D. (Guest)
on 2008-11-30 23:35
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 topic is locked and can not be replied to.