未定义的局部变量或方法
undefined local variable or method
我正在尝试将页面从布局呈现到我的电影索引,但问题是当我将代码放入索引时它工作正常但是当我将它移动到布局时,它显示 undefined local variable or method 'movie'
这是我的控制器:
def index
@movies = Movie.all.order(:cached_votes_score => :desc)
end
def show
end
def new
@movie = current_user.movies.build
@movie = Movie.new
@categories = Category.all
end
def edit
@categories = Category.all.map{|c| [ c.name, c.id ] }
end
def create
@movie = current_user.movies.build(movie_params)
@movie.category_id = params[:category_id]
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
@movie.category_id = params[:category_id]
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 upvote
@movie.upvote_from current_user, :vote_weight => 3
redirect_to root_path
end
def downvote
@movie.downvote_from current_user
redirect_to root_path
end
这是我的索引页:
<div class= "row">
<% @movies.each do |movie|%>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<%= link_to (image_tag movie.image.url(:medium), class: 'image'), movie %>
<%= render 'layouts/vote' %>
</div>
</div>
以及我要呈现的页面 layouts/vote
:
<%= link_to like_movie_path(movie), class: "like", method: :put do %>
<button type= "button" class = "btn btn-info" aria-label= "Left Align">
<span class= "glyphicon glyphicon-thumbs-up glyphicon-align-center" aria-hidden= "true"></span>
<span class= "badge"><%= movie.get_upvotes.size%></span>
</button>
<%end%>
<%= link_to dislike_movie_path(movie), class: "like", method: :put do %>
<button type= "button" class = "btn btn-info" aria-label= "Left Align">
<span class= "glyphicon glyphicon-thumbs-down glyphicon-align-center" aria-hidden= "true"></span>
<span class= "badge"><%= movie.get_downvotes.size%></span>
</button>
<%end%>
所以我得到了一个错误未定义的局部变量或方法`movie'
对于这一行:
<%= link_to like_movie_path(movie), class: "like", method: :put do %>
以及路线:
like_movie PUT /movies/:id/like(.:format) movies#upvote
dislike_movie PUT /movies/:id/dislike(.:format) movies#downvote
您需要将 movie
对象作为局部变量传递给 partial
<%= render 'layouts/vote', movie: movie %>
我正在尝试将页面从布局呈现到我的电影索引,但问题是当我将代码放入索引时它工作正常但是当我将它移动到布局时,它显示 undefined local variable or method 'movie'
这是我的控制器:
def index
@movies = Movie.all.order(:cached_votes_score => :desc)
end
def show
end
def new
@movie = current_user.movies.build
@movie = Movie.new
@categories = Category.all
end
def edit
@categories = Category.all.map{|c| [ c.name, c.id ] }
end
def create
@movie = current_user.movies.build(movie_params)
@movie.category_id = params[:category_id]
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
@movie.category_id = params[:category_id]
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 upvote
@movie.upvote_from current_user, :vote_weight => 3
redirect_to root_path
end
def downvote
@movie.downvote_from current_user
redirect_to root_path
end
这是我的索引页:
<div class= "row">
<% @movies.each do |movie|%>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<%= link_to (image_tag movie.image.url(:medium), class: 'image'), movie %>
<%= render 'layouts/vote' %>
</div>
</div>
以及我要呈现的页面 layouts/vote
:
<%= link_to like_movie_path(movie), class: "like", method: :put do %>
<button type= "button" class = "btn btn-info" aria-label= "Left Align">
<span class= "glyphicon glyphicon-thumbs-up glyphicon-align-center" aria-hidden= "true"></span>
<span class= "badge"><%= movie.get_upvotes.size%></span>
</button>
<%end%>
<%= link_to dislike_movie_path(movie), class: "like", method: :put do %>
<button type= "button" class = "btn btn-info" aria-label= "Left Align">
<span class= "glyphicon glyphicon-thumbs-down glyphicon-align-center" aria-hidden= "true"></span>
<span class= "badge"><%= movie.get_downvotes.size%></span>
</button>
<%end%>
所以我得到了一个错误未定义的局部变量或方法`movie' 对于这一行:
<%= link_to like_movie_path(movie), class: "like", method: :put do %>
以及路线:
like_movie PUT /movies/:id/like(.:format) movies#upvote
dislike_movie PUT /movies/:id/dislike(.:format) movies#downvote
您需要将 movie
对象作为局部变量传递给 partial
<%= render 'layouts/vote', movie: movie %>