所以我关注tutorial并使用云9作为我的编辑。当我运行我的程序并提交评论时。 Mackenzie(制作教程的人)评审表让观众选择星级作为他们的评级。另一方面,我的只允许用户输入一个数字,平均评级不会改变。
这是我的_form.html.erb
<%= form_for([@movie, @review]) do |f| %>
<% if @review.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@review.errors.count, "error") %> prohibited this review from being saved:</h2>
<ul>
<% @review.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :rating %><br>
<%= f.text_field :rating %>
</div>
<div class="field">
<%= f.label :comment %><br>
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
$('#star-rating').raty({
path: '/assets/',
scoreName: 'review[rating]'
});
</script>
我的评论控制器:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_movie
before_action :authenticate_user!
def new
@review = Review.new
end
def edit
end
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.movie_id = @movie.id
if @review.save
redirect_to @movie
else
render 'new'
end
end
def update
@review.update(review_params)
end
def destroy
@review.destroy
redirect_to root_path
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(:rating, :comment)
end
end
我确定我已经正确安装了raty。如果需要查看其他任何代码,请与我联系。所有帮助表示赞赏!