我试图将一个页面从布局渲染到我的电影索引但事情就是当我把代码放在索引中它工作正常但是当我将它移动到布局时,它显示我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&#39; 对于这一行:
<%= 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
答案 0 :(得分:2)
您需要将movie
对象作为局部变量传递给部分
<%= render 'layouts/vote', movie: movie %>