我正在尝试为视频创建新评论
视频观看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
create.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)
答案 0 :(得分:0)
尝试将文件重命名为create.js.coffee
。
它应该仍然呈现erb,尽管它没有写在文件扩展名中。
让我知道它是否对你有帮助