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 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持