Rails 4.2.1 审核记录正确,但不显示在展示页面
Rails 4.2.1 Reviews logging correctly, but not displaying on show page
一边学习一边开发电影评论应用 Rails。早些时候,我注意到所有页面上都出现了四篇影评,但没有出现新的影评。我查看了控制台,发现那四个评论没有附加到 movie_id(例如,每个评论都是 movie_id: nil)。因此,我使用 rails 控制台更新了这四个评论,将它们附加到特定的 movie_id。
现在所有电影都在放映 "No reviews just yet. Be the first to write one!",尽管实际上有些电影确实有相关评论(通过 rails 控制台中的 movie_id 验证)。
当我为特定电影添加评论时,评论记录正确,正如我在 rails 控制台中看到的那样:
<Review id: 13, rating: 3, comment: "Testing code to see if this review shows up.", created_at: "2015-05-26 21:57:52", updated_at: "2015-05-26 21:57:52", user_id: 1, movie_id: 2>
但是,电影显示页面在 show.html.erb 页面上仍然显示 "No reviews just yet. Be the first to write one!":
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-4">
<%= image_tag @movie.image.url(:medium) %>
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td><strong>Title:</strong></td>
<td><%= @movie.title %></td>
</tr>
<tr>
<td><strong>Description:</strong></td>
<td><%= @movie.description %></td>
</tr>
<tr>
<td><strong>Movie length:</strong></td>
<td><%= @movie.movie_length %></td>
</tr>
<tr>
<td><strong>Director:</strong></td>
<td><%= @movie.director %></td>
</tr>
<tr>
<td><strong>Rating:</strong></td>
<td><%= @movie.rating %></td>
</tr>
<tr>
<td><strong>Average User Rating:</strong></td>
<td><div class="star-rating" data-score= <%= @avg_rating %> ></div><p><%= "#{@reviews.length} reviews" %></p></td>
</tr>
</tbody>
</table>
<%= link_to "Write a Review", new_movie_review_path(@movie), class: "btn btn-primary" %>
</div>
</div>
<div class="col-md-7 col-md-offset-1">
<h1 class="review_title"><%= @movie.title %></h1>
<p><%= @movie.description %></p>
<% if @reviews.blank? %>
<h3>No reviews just yet. Be the first to write one!</h3>
<%= link_to "Write Review", new_movie_review_path(@movie), class: "btn btn-danger" %>
<% else %>
<% @reviews.each do |review| %>
<div class="reviews">
<div class="star-rating" data-score= <%= review.rating %> ></div>
<p><%= h(review.comment).gsub(/\n/, '<br/>').html_safe %></p>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
<%= link_to 'Edit', edit_movie_path(@movie) %> |
<%= link_to 'Back', movies_path %>
<script>
$('.star-rating').raty({
path: '/assets/',
readOnly: true,
score: function() {
return $(this).attr('data-score');
}
});
</script>
我认为 movies_controller:
中的所有定义都正确
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /movies
# GET /movies.json
def index
@movies = Movie.all
end
def search
if params[:search].present?
@movies = Movie.search(params[:search])
else
@movies = Movie.all
end
end
# GET /movies/1
# GET /movies/1.json
def show
@reviews = Review.where(movie_id: @movie_id).order("created_at DESC")
if @reviews.blank?
@avg_rating = 0
else
@avg_rating = @reviews.average(:rating).round(2)
end
end
# GET /movies/new
def new
@movie = current_user.movies.build
end
# GET /movies/1/edit
def edit
end
# POST /movies
# POST /movies.json
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
# PATCH/PUT /movies/1
# PATCH/PUT /movies/1.json
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
# DELETE /movies/1
# DELETE /movies/1.json
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
private
# Use callbacks to share common setup or constraints between actions.
def set_movie
@movie = Movie.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def movie_params
params.require(:movie).permit(:title, :description, :movie_length, :director, :rating, :image)
end
end
这也是我的 reviews_controller:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_movie
before_action :authenticate_user!
# GET /reviews/new
def new
@review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.movie_id = @movie.id
respond_to do |format|
if @review.save
format.html { redirect_to @movie, notice: 'Review was successfully created.' }
format.json { render :show, status: :created, location: @review }
else
format.html { render :new }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if @review.update(review_params)
format.html { redirect_to @movie, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: @review }
else
format.html { render :edit }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
@review.destroy
respond_to do |format|
format.html { redirect_to @movie, notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
@review = Review.find(params[:id])
end
def set_movie
@movie = Movie.find(params[:movie_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
我只是不太明白我在这里错过了什么。谁能看出为什么评论准确记录并显示在控制台中,但没有显示在电影页面上?
感谢您的帮助!!
评论未加载,因为您在显示操作中有错字:
@reviews = Review.where(movie_id: @movie_id).order("created_at DESC")
应该是:
@reviews = Review.where(movie_id: @movie.id).order("created_at DESC")
这是因为您设置了@movie 变量,而不是@movie_id 变量。
一边学习一边开发电影评论应用 Rails。早些时候,我注意到所有页面上都出现了四篇影评,但没有出现新的影评。我查看了控制台,发现那四个评论没有附加到 movie_id(例如,每个评论都是 movie_id: nil)。因此,我使用 rails 控制台更新了这四个评论,将它们附加到特定的 movie_id。
现在所有电影都在放映 "No reviews just yet. Be the first to write one!",尽管实际上有些电影确实有相关评论(通过 rails 控制台中的 movie_id 验证)。
当我为特定电影添加评论时,评论记录正确,正如我在 rails 控制台中看到的那样:
<Review id: 13, rating: 3, comment: "Testing code to see if this review shows up.", created_at: "2015-05-26 21:57:52", updated_at: "2015-05-26 21:57:52", user_id: 1, movie_id: 2>
但是,电影显示页面在 show.html.erb 页面上仍然显示 "No reviews just yet. Be the first to write one!":
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-4">
<%= image_tag @movie.image.url(:medium) %>
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td><strong>Title:</strong></td>
<td><%= @movie.title %></td>
</tr>
<tr>
<td><strong>Description:</strong></td>
<td><%= @movie.description %></td>
</tr>
<tr>
<td><strong>Movie length:</strong></td>
<td><%= @movie.movie_length %></td>
</tr>
<tr>
<td><strong>Director:</strong></td>
<td><%= @movie.director %></td>
</tr>
<tr>
<td><strong>Rating:</strong></td>
<td><%= @movie.rating %></td>
</tr>
<tr>
<td><strong>Average User Rating:</strong></td>
<td><div class="star-rating" data-score= <%= @avg_rating %> ></div><p><%= "#{@reviews.length} reviews" %></p></td>
</tr>
</tbody>
</table>
<%= link_to "Write a Review", new_movie_review_path(@movie), class: "btn btn-primary" %>
</div>
</div>
<div class="col-md-7 col-md-offset-1">
<h1 class="review_title"><%= @movie.title %></h1>
<p><%= @movie.description %></p>
<% if @reviews.blank? %>
<h3>No reviews just yet. Be the first to write one!</h3>
<%= link_to "Write Review", new_movie_review_path(@movie), class: "btn btn-danger" %>
<% else %>
<% @reviews.each do |review| %>
<div class="reviews">
<div class="star-rating" data-score= <%= review.rating %> ></div>
<p><%= h(review.comment).gsub(/\n/, '<br/>').html_safe %></p>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
<%= link_to 'Edit', edit_movie_path(@movie) %> |
<%= link_to 'Back', movies_path %>
<script>
$('.star-rating').raty({
path: '/assets/',
readOnly: true,
score: function() {
return $(this).attr('data-score');
}
});
</script>
我认为 movies_controller:
中的所有定义都正确 class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /movies
# GET /movies.json
def index
@movies = Movie.all
end
def search
if params[:search].present?
@movies = Movie.search(params[:search])
else
@movies = Movie.all
end
end
# GET /movies/1
# GET /movies/1.json
def show
@reviews = Review.where(movie_id: @movie_id).order("created_at DESC")
if @reviews.blank?
@avg_rating = 0
else
@avg_rating = @reviews.average(:rating).round(2)
end
end
# GET /movies/new
def new
@movie = current_user.movies.build
end
# GET /movies/1/edit
def edit
end
# POST /movies
# POST /movies.json
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
# PATCH/PUT /movies/1
# PATCH/PUT /movies/1.json
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
# DELETE /movies/1
# DELETE /movies/1.json
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
private
# Use callbacks to share common setup or constraints between actions.
def set_movie
@movie = Movie.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def movie_params
params.require(:movie).permit(:title, :description, :movie_length, :director, :rating, :image)
end
end
这也是我的 reviews_controller:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_movie
before_action :authenticate_user!
# GET /reviews/new
def new
@review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.movie_id = @movie.id
respond_to do |format|
if @review.save
format.html { redirect_to @movie, notice: 'Review was successfully created.' }
format.json { render :show, status: :created, location: @review }
else
format.html { render :new }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if @review.update(review_params)
format.html { redirect_to @movie, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: @review }
else
format.html { render :edit }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
@review.destroy
respond_to do |format|
format.html { redirect_to @movie, notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
@review = Review.find(params[:id])
end
def set_movie
@movie = Movie.find(params[:movie_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
我只是不太明白我在这里错过了什么。谁能看出为什么评论准确记录并显示在控制台中,但没有显示在电影页面上?
感谢您的帮助!!
评论未加载,因为您在显示操作中有错字:
@reviews = Review.where(movie_id: @movie_id).order("created_at DESC")
应该是:
@reviews = Review.where(movie_id: @movie.id).order("created_at DESC")
这是因为您设置了@movie 变量,而不是@movie_id 变量。