Search code examples
rubyrails-activestorage

undefined method 'image' for nil:class


Im trying to display all the images from active storage on an index page. Note: The image will display in the show page

Heres what i have on the index page:

<% @imagelists.each  do |il| %>
 <%= image_tag(@il.image) &>
<% end %>

I can do the following which shows the active storage data

<% @imagelists.each  do |il| %>
 <%= @il.image &>
<% end %>

Solution

  • When you do this:

    <% @imagelists.each  do |il| %>
    

    You are defining a variable il which is available within that each block.

    However, in your code you are referencing @il - an instance variable which doesn't exist. Unlike a normal variable, when you reference an instance variable which isn't defined, you just get nil, not a NoMethodError.

    So, just use il instead of @il and you'll be fine.