我正在尝试为视频创建新评论
视频视图中的show.html.erb
<h5>New Comment</h5>
<%= render 'comments/form' %>
<div class = "show-comments">
<% if @video.comments.size > 0 %>
<h5>Comments</h5>
<% for comment in @video.comments %>
<div class = "well">
<p><%= comment.content %></p>
<small><%= comment.updated_at %></small>
<% if comment.user_id == current_user.id %>
<p align="right"><%= link_to 'delete', comment, method: :delete, data: { confirm: 'Are you sure?' } %></p>
<% end %>
</div>
<% end %>
<% end %>
</div>
_form.html.erb在评论视图中
<%= form_for [@video, Comment.new], remote: true do |f| %>
<p><%= f.text_area :content, :rows => 3 %></p>
<p><%= f.submit %></p>
<% end %>
在 comments_controller.rb 中创建方法
def create
@video = Video.find(params[:video_id])
@comment = Comment.new(params[:comment])
current_user.comments << @comment
@video.comments << @comment
respond_to do |format|
if @comment.save
format.html { redirect_to @video, notice: 'Comment was successfully added.' }
format.js
end
end
end
创建.js.coffee.erb文件
new_comment = $('<p><%= @comment.content %></p>')
$(".show-comments").prepend(new_comment)
当我单击提交评论按钮时,它应该调用 javascript 并发布新评论,但它没有。
这是一条错误消息
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:3000/videos/51adc1ae25e218d5f6000001/comments
POST http://localhost:3000/videos/51adc1ae25e218d5f6000001/comments 500 (Internal Server Error)