Actors I step 6: undefined method `each' for nil:NilClass


#1

Getting this error. The error seems to indicate that there is no object called "filmography" when in fact there is, and I passed that step of the course.

Here's my show.html.erb file:

<div class="main actor-show">
  <div class="container">
    
    <!-- Display an actor's info here -->
    <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>

    <h2>Movies</h2>
    <% @filmography.each do |f| %>
    <div class="movie">
      <%= image_tag(f.image) %>
            <h3 class="movie-title"><%= f.title %></h3>
            <p class="movie-release-year"><%= f.release_year %></p>
            <p class="movie-plot"><%= f.plot %></p>
    </div>
    <% end %>

  </div>

Here's the 'actors#show' action:

def show
    @actor = Actor.find_by(params[:id])
    @filmography = @actor.movies
  end

Am I missing something dumb here? Again, all the previous steps turned up green.


#2

Hi Mike,

This was tricky - it took me a while to find the problem :)

It should either be find(...), or find_by_id(...), but there's no find_by that I know of :)


#3

Thanks for taking a look. I'm a beginner so might be missing something here, but the API docs seem to indicate that it is a valid method...no?

Further, I believe I used that syntax previously in another exercise in the course, and it validated and worked successfully.


#4

I think that you may be using find_by wrong. If you look at the example, it specifies what it's finding by, like:

Post.find_by name: 'Spartacus', rating: 4

so I think you would need something like:

Actor.find_by id: params[:id]

But I'm not quite sure if that will work. Do find and find_by_id work?