Movies II (5.3) 'Each' Error


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>
      <% end %>

    <% @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"><%= %></p>
		<% end %>


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


Hi Olivia,

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


Not a problem!

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



@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

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