StackExchange User
StackExchange User

Reputation: 1220

undefined method `map' for nil:NilClass in Rails with Paperclip

This will be my last question for today (sorry if I'm asking too fast)

I'm getting the error undefined method 'map' for nil:NilClass

It says the problem is on this line: <td><%= image_tag @map.map.url %></td>

The whole index code is below:

<h1>Listing maps</h1>

<table>
  <tr>
    <th>Carname</th>
    <th>Map</th>
    <th>Criticalcomponentlocations</th>
    <th>Warnings</th>
    <th>Additionalinfo</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @maps.each do |map| %>
  <tr>
    <td><%= map.carname %></td>
    <td><%= image_tag @map.map.url %></td>
    <td><%= map.criticalcomponentlocations %></td>
    <td><%= map.warnings %></td>
    <td><%= map.additionalinfo %></td>
    <td><%= link_to 'Show', map %></td>
    <td><%= link_to 'Edit', edit_map_path(map) %></td>
    <td><%= link_to 'Destroy', map, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Map', new_map_path %>

Maps Controller, Index:

  def index
    @maps = Map.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @maps }
    end
  end

Upvotes: 0

Views: 306

Answers (3)

zeantsoi
zeantsoi

Reputation: 26193

In your @maps.each loop, you're trying to access a (probably) non-existent @map instance variable when you should be accessing the map variable that is local to your loop.

Try this instead:

<% @maps.each do |map| %>
  <tr>
    ...
    <td><%= image_tag map.map.url %></td>
    ...
  </tr>
<% end %>

Upvotes: 1

tralston
tralston

Reputation: 3035

Your line of code <td><%= image_tag @map.map.url %></td> uses an instance variable @map, but you're inside the scope of the enumerator <% @maps.each do |map| %>. You should use the map.url local variable instead of @map.map.url.

Upvotes: 0

salexander
salexander

Reputation: 954

The variable you wish to use, map, is not an instance variable. It is a local variable, so you should use 'map.url' instead of '@map.map.url'

Upvotes: 0

Related Questions