Bootstrap模态形式提交两次 [英] Bootstrap modal form is committing twice

查看:91
本文介绍了Bootstrap模态形式提交两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模态形式:

<div id="commentModal" class="modal hide fade">
  <div class="modal-body">
    <%= form_for(Comment.new, remote: true, html: {"data-type" => :json}, :validate => true) do |f| %>
      <%= f.hidden_field(:illustration_id, :value => @illustration.id) %>
      <%= f.hidden_field(:user_id, :value => current_user.id) %> 
      <%= f.text_area(:comment, :id => "comment_message") %>
      <%= f.submit "Submit", :class => 'btn btn-custom-primary' %>
    <% end %>

  </div>
</div>

当我提交注释两次添加到数据库时,这是日志.你知道为什么会这样吗?我可以提供其他代码来帮助您吗?

When I submit the comment is added to the db twice, here is the log. Do you see why this is happening? Can I provide any other code to help?

Started POST "/comments" for 127.0.0.1 at 2013-07-13 10:58:32 -0400
Processing by CommentsController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"asdfasdfasdfasdfasasdfasdfasdfasdfasdf", "comment"=>{"illustration_id"=>"1", "user_id"=>"1", "comment"=>"Test comment"}, "commit"=>"Submit"}
   (36.8ms)  BEGIN
  SQL (78.6ms)  INSERT INTO "comments" ("comment", "created_at", "illustration_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["comment", "Test comment"], ["created_at", Sat, 13 Jul 2013 14:58:32 UTC +00:00], ["illustration_id", 1], ["updated_at", Sat, 13 Jul 2013 14:58:32 UTC +00:00], ["user_id", 1]]
   (38.7ms)  COMMIT
Completed 201 Created in 163ms (Views: 1.4ms | ActiveRecord: 154.1ms)


Started POST "/comments" for 127.0.0.1 at 2013-07-13 10:58:32 -0400
Processing by CommentsController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"asdfasdfasdfasdfasasdfasdfasdfasdfasdf", "comment"=>{"illustration_id"=>"1", "user_id"=>"1", "comment"=>"Test comment"}, "commit"=>"Submit"}
   (36.5ms)  BEGIN
  SQL (36.9ms)  INSERT INTO "comments" ("comment", "created_at", "illustration_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["comment", "Test comment"], ["created_at", Sat, 13 Jul 2013 14:58:32 UTC +00:00], ["illustration_id", 1], ["updated_at", Sat, 13 Jul 2013 14:58:32 UTC +00:00], ["user_id", 1]]
   (38.3ms)  COMMIT
Completed 201 Created in 116ms (Views: 0.9ms | ActiveRecord: 111.7ms)

根据要求,注释控制器创建方法:

As requested, the comment controller create method:

def create
    @comment = Comment.new(params[:comment])

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

和新方法:

def new
    @comment = Comment.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @comment }
    end
  end

推荐答案

我遇到了这个问题,结果很偷偷摸摸.我在模态中渲染了一个表单,并且提交了两次.最终结果是,当我渲染该内容时,我使用的是布局,因此又包含了所有的JS等-有效地双重绑定了表单.真正令人惊讶的是,表单呈现得很好-显然没有在模态中显示我的应用程序的其余部分.

I ran into this issue and it ended up being pretty sneaky. I was rendering a form inside a modal and it was submitting twice. It ended up being that when I was rendering that content, I was doing so with a layout and thus including all the JS etc again - effectively double-binding the form. What was really surprising is that the form rendered fine - not obviously showing the rest of my app inside the modal.

我通过在没有布局的情况下渲染模态来修复它.在我的控制器中:

I fixed it by rendering the modal without a layout ie. in my controller:

render layout: false

这篇关于Bootstrap模态形式提交两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆