5.3 Movies II


#1

EXERCISE LINK

ERROR:
undefined local variable or method `movie' for #<#:0x000000057db428>

CODE:
Show.html.erb

<div class="main movie-show">
  <div class="container">
    <div class="movie">
      <div class="info">
        	<%= @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>
    	<div class="actor">
        <% @actor.each do |a| %>
      		<%= @actor.image %>
      		<h3 class="actor-name"> <%= actor.first_name %> </h3>
      		<p class="actor-bio"> <%= actor.bio %> </p>
    		<% end %>
    	</div>
  </div>
</div>

movies_controller.rb

class MoviesController < ApplicationController
  def index
    @movies = Movie.all
  end

  def show
    @movie = Movie.find(params[:id])
    @actors = @movie.actors
  end
end

routes.rb

Rails.application.routes.draw do
  get "/movies" => "movies#index"
  get "/movies/:id" => "movies#show", as: :movie
end

#2

Could it be you are missing the @ symbol on those three lines?


#3

@mtf Yes, that's correct! Now I have this error:

"undefined method `image' for nil:NilClass"

After adding the "@" before actor & movies when displaying their qualities.


#4

This line is missing the image_tag directive.


#5

@mtf I'm still getting the same error, after making that change.


#6

'directive' might not be the right word, 'attribute' may better describe it.

<%= image_tag @movie.image %>

#7

@mtf That's what I wrote:

Show.html.erb

<div class="main movie-show">
  <div class="container">
    <div class="movie">
        	<%= 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 class="info">
      </div>
    </div>

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

movies_controller.rb

class MoviesController < ApplicationController
  def index
    @movies = Movie.all
  end

  def show
    @movie = Movie.find(params[:id])
    @actors = @movie.actors
  end
end

routes.rb

Rails.application.routes.draw do
  get "/movies" => "movies#index"
  get "/movies/:id" => "movies#show", as: :movie
end

ERROR MESSAGE:
"undefined method `image' for nil:NilClass"


#8

I'm going to invite my go to guy to this topic and see if he can't be more helpful. I'm not seeing any other issues, just now.


#9

Thanks @mtf . Much appreciated! =D


#10

@snaylz Inside your @actors.each do... loop, a is your iterator, so you need to access a.image, a.first_name, etc. instead of @actor.whatever :slight_smile:


#11

Hey @zystvan - I'm still getting the same error message, while using this code:

show.html.erb

<div class="main movie-show">
  <div class="container">
    <div class="movie">
        	<%= 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 class="info">
      </div>
    </div>

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

ERROR MESSAGE:
"undefined method `image' for nil:NilClass"


#12

@snaylz Remove the @, since it's not part of your variable, and you should be good to go :slight_smile:


#13

@zystvan You are a genius!

Thanks @mtf for bringing him in to help out on this tricky one for us. :smiley:


#14

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