Rails的Ajax 500服务器错误
我想获得新的评论用ajax而不是页面重新加载更新。我遵循railscast教程,但在我的js控制台中出现500内部服务器错误。我正在阅读其他帖子,很多人都说这是一个部分错误,但我无法弄清楚。注释将重新加载页面之前保存,但不会显示,直到重新加载页面。这里是注释控制器Rails的Ajax 500服务器错误
def create
@post = Post.find(params[:post_id])
@comment = Comment.create(params[:comment].permit(:content))
@comment.user_id = current_user.id
@comment.post_id = @post.id
if @comment.save
respond_to do |format|
format.html {redirect_to post_path(@post.comments)}
format.js
end
else
render 'new'
end
end
在注释目录中的文件create.js.erb。
$('.comment').append('<%= j render @post.comments %>');
评论表单
<%= simple_form_for([@post, @post.comments.build], :remote => true, :input_html => { :data => { :url => '/post/:id', :type => :js } }) do |f| %>
<%= f.input :content, label: "Reply"%>
<%= f.button :submit, "Submit" %>
<% end %>
我不是100%肯定,如果这是你目前的问题,但format.html {redirect_to post_path(@post.comments)}
试图去post
路径都属于文章的评论,这是没有意义的。我认为你的意思是去post
的道路。
format.html { redirect_to post_path(@post) }
如果是一个局部问题,那么here is a stack overflow question这将可能是有益的。
也有可能你对你的部分犯了同样的错误,你应该通过@post
而不是@post.comments
。或者,也许你应该通过最新的评论,而不是所有的评论。
编辑:
您可能需要指定在JavaScript部分:
$('.comment').append('<%= j render 'comments/form', locals: {post: @post} %>');
而且在您的评论的形式,我相信你改变所有的@post
s到post
秒。
这里是another stack overflow question which may be helpful
EDIT2:
尝试了这一点。这与我最后一次编辑的区别在于,不是将表单放在评论上,而是现在将表单放在帖子上。
# app/controllers/comments_controller.rb
def create
@post = Post.find(params[:post_id])
@comment = Comment.create(params[:comment].permit(:content))
@comment.user_id = current_user.id
@comment.post_id = @post.id
if @comment.save
respond_to do |format|
format.html { redirect_to post_path(@post) }
format.js
end
else
render 'new'
end
end
# app/views/posts/show.html.erb
# Doesn't need to look exactly like this
<div class='post'>
<div class='content'>
<%= @post.content %>
</div>
<div class='form'>
<%=j render 'comments/form', locals: { post: @post } %>
</div>
</div>
<div class='comment'>
<%= render @post.comments %>
</div>
# app/views/comments/create.js.erb
$('.comment').append('<%=j render @post.comments %>');
# app/views/comments/_comment.html.erb
<%= comment.content %>
# app/views/posts/_form.html.erb
<%= simple_form_for([@post, @post.comments.build], :remote => true, :input_html => { :data => { :url => '/post/:id', :type => :js } }) do |f| %>
<%= f.input :content, label: "Reply"%>
<%= f.button :submit, "Submit" %>
<% end %>
#config/routes.rb
resources :posts do
resources :comments #-> url.com/posts/:post_id/comments/new
end
#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
respond_to :js, :html, only: :create #-> requires "responders" gem
def create
@post = Post.find params[:post_id]
@comment = @post.comments.new comment_params
respond_with @comment
end
private
def comment_params
params.require(:comment).permit(:content).merge(user_id: current_user.id)
end
end
#app/views/comments/create.js.erb
$('.comment').append('<%=j render @post.comments %>'); #-> requires comments/_comment.html.erb
#app/views/comments/_comment.html.erb
<%= comment.content %>
#app/views/posts/show.html.erb
<%= simple_form_for [@post, @post.comments.new], remote: true do |f| %>
<%= f.input :content, label: "Reply"%>
<%= f.button :submit, "Submit" %>
<% end %>
以上是它应该如何工作。
我全都写出来给出正确的上下文等。
什么是评论形式命名? – Dbz
Just _form.html.erb – nharvey27
有关此状态的任何更新? – Dbz