尝试将电影评论添加到我的电影应用中。但是当任何人都可以添加评论时,它不会显示在页面上
rails console显示评论和评级
=> #<Review id: 3, **movie_rating: 2, comment: "test",** created_at: "2015-02-12 11:20:10", updated_at: "2015-02-12 11:20:10", user_id: 2, movie_id: nil>
但未出现在我的show.html.erb页
上<div class="col-md-9">
<% if @reviews.blank? %>
<h3>No reviews yet. Be the first to write one!</h3>
<% else %>
<% @reviews.each do |review| %>
<p><%= review.movie_rating %></p>
<p><%= review.comment %></p>
<% end %>
<% end %>
</div>
<%= link_to 'Edit', edit_movie_path(@movie), class: "btn btn-link" %> |
<%= link_to 'Back', movies_path, class: "btn btn-link" %>
并在show
下的movies_controller页面中定义了这个class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
# GET /movies
# GET /movies.json
def index
@movies = Movie.all
end
# GET /movies/1
# GET /movies/1.json
def show
**@reviews = Review.where(movie_id: @movie.id)**
end
# GET /movies/new
def new
@movie = Movie.new
end
# GET /movies/1/edit
def edit
end
# POST /movies
# POST /movies.json
def create
@movie = Movie.new(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
review_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [:edit, :update, :destroy]
before_action :set_movie
before_action :authenticate_user!
respond_to :html
def new
@review = Review.new
respond_with(@review)
end
def edit
end
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.movie_id = @movie_id
@review.save
respond_with(@movie)
end
def update
@review.update(review_params)
respond_with(@review)
end
def destroy
@review.destroy
respond_with(@review)
end
private
def set_review
@review = Review.find(params[:id])
end
def set_movie
@movie = Movie.find(params[:movie_id])
end
def review_params
params.require(:review).permit(:movie_rating, :comment)
end
end
答案 0 :(得分:2)
您是否注意到 movie_id
中生成的 log
nil
?是因为@review.movie_id = @movie_id
create
方法中reviews_controller
的这一行@review.movie_id = @movie.id
是错误。它应该是def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.movie_id = @movie.id #here
@review.save
respond_with(@movie)
end
。
reviews
这就是movie_id
没有显示的原因,因为他们有{{1}} 无。