Movies II (5.3) 'Each' Error


#1

Getting an error for 'undefined method 'each' for nil:NilClass'. Here's my code for /show.html.erb, thanks y'all:

<div class="main movie-show">
  <div class="container">
    <div class="movie"> <% @movies.each do |movie| %>
      <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>
      <% end %>
    </div>

    <h2>Cast</h2>
    <% @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 %>
  </div>
</div>

#2

Just an FYI, it's not showing any of my html for the movies section. Not sure what's up with that....


#3

Hi Olivia,

Could I see your Movies controller code, please? It's in app/controllers/movies_controller.rb :slight_smile:


#4

Not a problem!

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

Thanks!


#5

@teraninja09473 Thanks :slight_smile: - you need to show details on @movie without looping through @movies (which doesn't exist in your Show action).

For example,

<%= image_tag @movie.image %>
<!-- ... -->

rather than:

<% @movies.each do |movie| %>
  <%= image_tag movie.image %>
  <!-- ... -->
<% end %>

Is that clear enough?


Undefined method `each' for nil:NilClass
#6

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