/ / "Niezdefiniowana metoda" title "for nil: NilClass" błąd w Ruby on Rails - ruby-on-rails

"Niezdefiniowana metoda" title "for nil: NilClass" błąd w Ruby on Rails - ruby-on-rails

Buduję stronę wideo i dostaję ten błąd, gdy próbuję dodać tytuł do mojej statycznej strony, na której użytkownicy mogą oglądać wideo:

watch.html.erb

<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-4">
<p>You"re watching:</p>
<h1><%= @movie.title %></h1>
</div>
</div>
</div>

I oto mój plik movies_controller.rb:

class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]

def watch
end

def index
@movies = Movie.all
end

def show
@reviews = Review.where(movie_id: @movie.id).order("created_at DESC")
end

def new
@movie = current_user.movies.build
end

def edit
end

def create
@movie = current_user.movies.build(movie_params)

respond_to do |format|
if @movie.save
format.html { redirect_to @movie, notice: "Movie was successfully created." }
format.json { render :show, status: :created, location: @movie }
else
format.html { render :new }
format.json { render json: @movie.errors, status: :unprocessable_entity }
end
end
end

def update
respond_to do |format|
if @movie.update(movie_params)
format.html { redirect_to @movie, notice: "Movie was successfully updated." }
format.json { render :show, status: :ok, location: @movie }
else
format.html { render :edit }
format.json { render json: @movie.errors, status: :unprocessable_entity }
end
end
end

def destroy
@movie.destroy
respond_to do |format|
format.html { redirect_to movies_url, notice: "Movie was successfully destroyed." }
format.json { head :no_content }
end
end

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

private
def movie_params
params.require(:movie).permit(:title, :description, :movie_length,     :director, :rating, :image)
end
end

Moja metoda oglądania jest powyżej "prywatna" w mojejplik kontrolera, który powiedziałem, jest jedną z głównych przyczyn tego błędu, ale za każdym razem, gdy próbuję przejść do strony, błąd nadal się pojawia. Czy ktoś może mi powiedzieć, co robię źle?

Odpowiedzi:

1 dla odpowiedzi № 1

Komunikat o błędzie jest bardzo jasny: @movie jest nil. Nigdzie w twoim watch czy spróbujesz zdefiniować tę zmienną jako coś innego niż nil, I twój before_action wywołanie zwrotne, które się zapełnia @movie bardzo wyraźnie nie obejmuje watch na liście działań, dla których działa.