Bootstrap modal form is committing twice

╄→гoц情女王★ 提交于 2020-01-07 04:34:07

问题


I have a modal form:

<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

And new method:

def new
    @comment = Comment.new

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

回答1:


I think your problem is remote :true for ajax submission call. Remove remote :true option from form tag and try normal form submit..surely it works

otherwise If u need ajax submit, please refer this Jquery Rails 3... form submits twice... deletes twice... help




回答2:


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


来源:https://stackoverflow.com/questions/17631399/bootstrap-modal-form-is-committing-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!