weixin_33709590 2016-01-22 00:35 采纳率: 0%
浏览 41

Rails Ajax 500服务器错误

I am trying to get new comments to update with ajax instead of a page reload. I followed the railscast tutorial but am getting a 500 internal server error in my js console. I was reading other posts and a lot of people are saying its a partial error but I can't figure it out. The comment will save before page reload but will not display until the page is reloaded.. Here is the comment controller

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

The create.js.erb file in the comment directory.

$('.comment').append('<%= j render @post.comments %>');

The comment form

<%= 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条回答 默认 最新

  • weixin_33720078 2016-01-22 00:59
    关注

    I'm not 100% sure if this is your current issue, but format.html {redirect_to post_path(@post.comments)} is trying to go to the post path for all the comments belonging to a post, which doesn't make sense. I think what you mean to do is go to the post's path.

    format.html { redirect_to post_path(@post) }

    If this is a partial problem, then here is a stack overflow question which will likely be helpful.

    It's also possible you are making the same mistake with your partial, and you should be passing it @post instead of @post.comments. Or perhaps you should be passing it the latest comment instead of all of the comments.

    EDIT:

    You may need to specify the partial in your javascript:

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

    And in your comment form, I believe you change all the @posts to posts.

    Here is another stack overflow question which may be helpful

    EDIT2:

    Try this out. The difference between this and my last edit is that instead of putting the form on the comments, now you put the form on the post.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀