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>

コメントを送信すると、データベースに 2 回追加されます。ログは次のとおりです。なぜこれが起こっているのか分かりますか?他に役立つコードを提供できますか?

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)

要求に応じて、コメント コントローラーの create メソッド:

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
4

2 に答える 2

0

あなたの問題はリモートだと思います: ajax送信呼び出しの場合はtrueです。form タグから remote :true オプションを削除し、通常のフォーム送信を試してください。

それ以外の場合は、ajax送信が必要な場合は、このJquery Rails 3を参照してください...フォームが2回送信されます...2回削除されます...ヘルプ

于 2013-07-14T15:32:45.733 に答える
0

私はこの問題に遭遇し、かなり卑劣になりました。モーダル内でフォームをレンダリングしていて、2 回送信していました。結局、そのコンテンツをレンダリングしていたときに、レイアウトを使用してレンダリングしていたため、すべての JS などを再度含めて、フォームを効果的に二重バインドしていました。本当に驚いたのは、フォームがうまくレンダリングされたことです。モーダル内にアプリの残りの部分が明らかに表示されていません。

レイアウトなしでモーダルをレンダリングすることで修正しました。私のコントローラーで:

render layout: false
于 2013-12-22T22:09:29.667 に答える