Rails Ajax 500服务器错误

时间:2016-01-22 00:35:45

标签: jquery ruby-on-rails ajax

我正在尝试使用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

评论目录中的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 %>

2 个答案:

答案 0 :(得分:0)

我不能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中指定partial:

$('.comment').append('<%= j render 'comments/form', locals: {post: @post} %>');

在您的评论表单中,我认为您将所有@post更改为post s。

这是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 %>

答案 1 :(得分:0)

#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 %>

以上是它应该如何运作。

我全力以赴写出适当的背景等。