2013-07-13 36 views
0

我有一個模式窗體:引導模式窗體犯兩次

<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> 

當我提交的評論被添加到數據庫兩次,這裏是日誌。你明白爲什麼會發生這種情況嗎?我可以提供任何其他代碼來幫助嗎?

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) 

按照要求,評論控制器創建方法:

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 
+0

你可以發佈你的控制器代碼嗎? –

+0

@ErminDedovic,補充說。 –

+0

你有任何發生的動作(過濾器)之前或之後遞交? –

回答

0

我就遇到了這個問題,它結束了是非常狡猾的。我在一個模式中呈現一個表單,它提交了兩次。它結束了,當我渲染的內容,我用的佈局這樣做,因此包括所有的JS等再次 - 有效的雙結合的形式。真正令人驚訝的是,表單呈現正常 - 沒有明顯地顯示我的應用程序在模態內的其餘部分。

我通過渲染模式沒有佈局,即固定它。在我的控制器中:

render layout: false