Jquery Rails Ajax 500服务器错误

Jquery Rails Ajax 500服务器错误,jquery,ruby-on-rails,ajax,Jquery,Ruby On Rails,Ajax,我试图用ajax更新新的评论,而不是重新加载页面。我遵循了railscast教程,但在我的js控制台中出现了500个内部服务器错误。我在读其他帖子,很多人都说这是一个局部错误,但我无法理解。该注释将在重新加载页面之前保存,但在重新加载页面之前不会显示。。这是注释控制器 def create @post = Post.find(params[:post_id]) @comment = Comment.create(params[:comment].permit(:content)) @

我试图用ajax更新新的评论,而不是重新加载页面。我遵循了railscast教程,但在我的js控制台中出现了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
comment目录中的create.js.erb文件

$('.comment').append('<%= j render @post.comments %>');
$('.comment')。追加('');
评论表

<%= 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 %>
true,:input_html=>{:data=>{:url=>'/post/:id',:type=>:js})do | f |%>

我不能100%确定这是否是您当前的问题,但是
format.html{redirect\u to post\u path(@post.comments)}
正在尝试为属于某篇文章的所有评论转到
post
路径,这没有意义。我想你的意思是去
帖子的路径

format.html{重定向到post路径(@post)}

如果这是一个局部问题,那么这可能会有所帮助

也有可能你在部分内容上犯了同样的错误,你应该传递它
@post
,而不是
@post.comments
。或者你应该给它传递最新的评论,而不是所有的评论

编辑:

您可能需要在javascript中指定分部代码:

$('.comment')。追加('')

在您的评论表中,我相信您将所有的
@post
s更改为
post
s

这是

编辑2:

试试这个。这次编辑与我上次编辑的不同之处在于,你没有将表单放在评论上,而是将表单放在帖子上

# 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 %>
#app/controllers/comments_controller.rb
def创建
@post=post.find(参数[:post\u id])
@comment=comment.create(参数[:comment].permit(:content))
@comment.user\u id=当前用户id
@comment.post_id=@post.id
if@comment.save
回应待办事项|格式|
format.html{redirect_to post_path(@post)}
format.js
结束
其他的
呈现“新”
结束
结束
#app/views/posts/show.html.erb
#不需要看起来像这样
#app/views/comments/create.js.erb
$('.comment')。追加('');
#app/views/comments/_comment.html.erb
#app/views/posts/_form.html.erb
true,:input_html=>{:data=>{:url=>'/post/:id',:type=>:js})do | f |%>
#config/routes.rb
资源:员额可以
参考资料:comments#->url.com/posts/:post_id/comments/new
结束
#app/controllers/comments\u controller.rb
类CommentsController需要“responders”gem
def创建
@post=post.find参数[:post_id]
@comment=@post.comments.new comment_参数
用@comment回复你
结束
私有的
def注释参数
参数require(:comment).permit(:content).merge(用户标识:当前用户标识)
结束
结束
#app/views/comments/create.js.erb
$('.comment')。附加('')#->需要注释/_comment.html.erb
#app/views/comments/_comment.html.erb
#app/views/posts/show.html.erb
以上是它应该如何工作


我写出来是为了提供适当的上下文等。

评论表单的名称是什么?这方面有什么更新吗?自从我编辑我的帖子以来,有什么变化吗?新的错误消息?我提供的任何解决方案都有帮助吗?谢谢你的帖子!我现在不在家里的电脑上,但我一回来就会给你更新。所以现在它会正确地添加评论而不重新加载页面,但现在所有的评论都有一个来自它们的创建评论,使网站看起来很糟糕。有什么建议吗?@nharvey27,原因是你正在抓取所有的评论,并用以下行将表单放到上面:
$('.comment').append('')。第一部分
$('.comment')
将获取所有注释,因此,如果您不想将表单放在所有注释上,则必须对其进行更改。也许你想把表单放在所有的帖子上?如果整个部分都是
,那么评论的部分真的有必要吗?如果他正在调用
:)我将其更改为您指定的方式,现在得到了一个例外:未定义的方法“comments\u url”用于#好的,这是
回复#你能试试吗
回复(@post,@comment)
?@RichPeck谢谢。你也在帮我做导轨,所以请回答我的问题!
#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 %>