Movies II 5/7: undefined 'name' for nil:NilClass


#1

I’ve been browsing the forums and have found a lot of solutions to my syntax errors but I’m still having trouble solving the error I get in the title of this post: “undefined ‘name’ for nil:NilClass”. I’ve searched my files for the “name” method but have not been able to locate it. I submitted a bug report after seeing a similar post with no resolution. It was dated for 2017 so I was hoping to have some help troubleshooting or find out whether there’s a work around besides the “Solution” button. Using the “Solution” button also hasn’t filled in the code it just clears the steps for me to move forward.

Controller

class MoviesController < ApplicationController
	def index
		@movies = Movie.all
	end
  
  def show
    @movie = Movie.find(params[:id])
    @actors = @movie.actors
  end
end

show.html.erb for Movies

        <!-- movie image -->
        <%= image_tag @movie.image %>
        <h3 class="movie-title"><%= @movie.title%>  </h3>
        <p class="movie-release-year"><%= @movie.release_year %>  </p>
        <p class="movie-plot"><%= @movie.plot %> </p>
      </div>
    </div>

    <h2>Cast</h2>
    <!-- Display each actor's info here -->
    <% @actors.each do |a| %>
    <div class="actor">
      <!-- actor image -->
      <%= a.image %>
      <h3 class="actor-name"><%= a.first_name + ' ' + a.last_name %> </h3>
      <p class="actor-bio"> <%= a.bio %> </p>
    <% end %>
    </div>

#3

The block parameter is a, but could as easily be actor

@plural.each do |singular|

Then use the parameter inside the loop.

actor.image
actor.first_name
actor.last_name
actor.bio

Let us know if this solves the problem.


#4

I updated this in my followup reply and it does not solve the error.


#5

Ah, didn’t see that, earlier. The structure looks like it is chained. There is an opentag but no endtag for the div.


#6

I updated the original post with my current code, I’ll delete the reply post with the snippet.


#7

As suspected, there is a chain. Write the endtag inside your loop, not after.


#8

I’m assuming your mean for the snippet to end like this

</div>
<% end %>

If so, I have done that and I’m still receiving the error “undefined method 'name for nil:NilClass”.


#9

I’m having a hard time finding this exercise. Please post a link.


#10

https://www.codecademy.com/courses/learn-rails/lessons/many-to-many/exercises/many-to-many-movies-ii?action=resume_content_item


#11
index.html.erb
          <% @actors.each do |actor| %>
          <div class="actor col-xs-2">
            <%= image_tag actor.image %>
            <h3><%= actor.first_name %> <%= actor.last_name %> </h3>
            <p><%= actor.bio %></p>
            <%= link_to "Learn more", actor_path(actor) %>
          </div>
          <% end %>

Your code is missing image_tag.

show.html.erb
    <div class="actor">
      <%= image_tag @actor.image %>
      <div class="info">
        <h3 class="actor-name"><%= @actor.first_name %> <%= @actor.last_name %></h3>
        <p class="actor-bio"><%= @actor.bio %></p>
      </div>
    </div>

#12

I have updated my code in the lesson and adding “image_tag” still does not resolve the error.


#13
movies/show.html.erb
    <h2>Cast</h2>
    <!-- Display each actor's info here -->
    <% @actors.each do |actor| %>
    <div class="actor">
      <%= image_tag actor.image %>
      <h3 class="actor-name"><%= actor.first_name %> <%= actor.last_name %>></h3>
      <p class="actor-bio"><%= actor.bio %></p>
    </div>
    <% end %>

Had the wrong example, above, from ACTORS. This one is MOVIES.


#14

I believe my code reflects everything so far.

      <div class="info">
        <%= image_tag @movie.image %>
        <h3 class="movie-title"><%= @movie.title %>  </h3>
        <p class="movie-release-year"><%= @movie.release_year %>  </p>
        <p class="movie-plot"><%= @movie.plot %>  </p>
      </div>
    </div>

    <h2>Cast</h2>
    <% @actors.each do |a| %>
    <div class="actor">
      <%= image_tag a.image %>
      <h3 class="actor-name"><%= a.first_name + ' ' + a.last_name %></h3>
      <p class="actor-bio"><%= a.bio %></p>
    </div>
   <% end %>

#15

@darrellw2210 Which URL gives you the error?

If you have an errant .name in your code, running this command in the terminal ought to show it:

$ grep -rn '\.name'
# ignore this line, anything else is probably your issue:
# public/index.html:98:      #about-content td.name  {color: #555}

If everything looks good, I’d suggest resetting the exercise and trying to go through again.


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.