所以我决定在 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
这种事经常发生在我身上。
关于javascript - Ruby On Rails Ajax 提交 提交两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29088860/