Ruby On Rails Ajax 提交提交两次

Ruby On Rails Ajax Submit submitting twice

本文关键字:提交 两次 Rails Ajax Ruby On      更新时间:2023-09-26

所以我决定在 RoR 中制作一个博客,一切都很好,我收到了要提交的评论,但当我去"更新"提交方法以允许在发布评论时不刷新页面(ajax)但是现在每当我出于某种原因在页面上发表评论时,它都会循环从评论提交发送,我得到两条评论同时出现相同的消息。

   <h2>Comments</h2>
 <div id="comments">
   <%= render :partial => @post.comments %>
 </div>
 <%= form_for [@post, Comment.new], :remote => true do |f| %>
   <p>
     <%= f.label :body, "New comment" %><br/>
     <%= f.text_area :body %>
   </p>
   <p><%= f.submit "Add comment", disable_with: "Adding Comment..." %></p>
 <% end %>

这就是显示点,如您所见,我试图查看禁用disable_with按钮是否有帮助,但没有。

下面是comment_controller

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create!(comment_params)
    respond_to do |format|
        format.html { redirect_to @post }
        format.js
      end
   end
        private
        def comment_params
                params.require(:comment).permit(:commenter, :body, :post_id)
        end
  end 

感谢您的任何建议。

看起来您有两组预编译资产在本地运行:

在您的计算机上尝试此操作:

RAILS_ENV=development bundle exec rake assets:clobber
RAILS_ENV=development bundle exec rake assets:precompile

一直发生在我身上。