【问题标题】:Rails Ajax 500 Server ErrorRails Ajax 500 服务器错误
【发布时间】:2016-04-28 11:33:35
【问题描述】:

我正在尝试让新的 cmets 使用 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 %>

【问题讨论】:

  • 评论表单叫什么?
  • 这个状态有什么更新吗?

标签: jquery ruby-on-rails ajax


【解决方案1】:
#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 %>

以上是它应该如何工作的。

我把它全部写出来以提供适当的上下文等。

【讨论】:

  • 如果整个部分是&lt;%= comment.content %&gt;,那么评论的部分真的有必要吗?
  • 如果他打电话给&lt;%= render @post.comments %&gt; :)
  • 我将其更改为您指定的方式,现在出现异常:#<0x007f8de46e0e00>
【解决方案2】:

我不能 100% 确定这是否是您当前的问题,但 format.html {redirect_to post_path(@post.comments)} 正在尝试转到属于某个帖子的所有 cmets 的 post 路径,这没有任何意义。我想你的意思是去post的路径。

format.html { redirect_to post_path(@post) }

如果这部分问题,那么here is a stack overflow question 可能会有所帮助。

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

编辑:

您可能需要在 javascript 中指定部分内容:

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

并且在您的评论表单中,我相信您将所有@posts 更改为posts。

这里是another stack overflow question which may be helpful

EDIT2:

试试这个。这与我上次编辑的区别在于,现在您将表单放在帖子上,而不是放在 cmets 上。

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

【讨论】:

  • 自从我编辑了我的帖子后有什么变化吗?新的错误信息?我提供的解决方案有帮助吗?
  • 感谢您的帖子!我现在不在家里的电脑旁,但我一回来就会给你更新。
  • 所以现在它会在不重新加载页面的情况下更正附加评论,但现在所有 cmets 都有一个创建评论,使网站看起来很糟糕。有什么建议吗?
  • @nharvey27,原因是因为您正在抓取所有 cmets 并将表格放在上面:$('.comment').append('&lt;%= j render @post.comments %&gt;');。第一部分$('.comment') 将获取所有 cmets,因此如果您不想将表单放在所有 cmets 上,更改它很重要。也许你想把表格放在所有的帖子上?
猜你喜欢
  • 2014-03-13
  • 2020-02-12
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 2014-09-24
  • 2018-08-09
相关资源
最近更新 更多