2

トランザクションを作成する ajax ボタンを作成しましたが、クリックしてTransaction.countも増加しません。

と の 3 つのモデルがUserありProductます。Transaction

ユーザーと製品は、両方に属するトランザクションを通じて多対多の関連付けを持っています。

これが私のapp/controllers/transactions_controller.rbの create メソッドです

def create
  @product = Product.find(params[:transaction][:product_id])
  @transaction = Transaction.new(transaction_params)
  @transaction.save!
  respond_to do |format|
    format.html { redirect_to @product }
    format.js
  end
end

def transaction_params
  params.require(:transaction).permit(:product_id, :start_date, :end_date)
end

ここに私のボタンビューapp/controllers/transactions/buttons/_wants.html.erb があります

<div class="wants_button product_<%=product.id%>">
  <% if current_user %>
    <%= form_for(current_user.transactions.build(product_id: product.id),
                                              remote: true) do |f| %>
      <div><%= f.hidden_field :product_id %></div>
      <%= f.submit t('button.ownership.create'), class: "btn btn-success btn-small" %>
    <% end %>
  <% end %>
</div>

そしてここで私のapp/views/transactions/create.js.erb

$(".wants_button.product_<%=@product.id%>").html("<%= escape_javascript(render('transactions/ownership/buttons/wants_not', product: @product)) %>")

少なくとも、サーバーを返すもの:

Started POST "/transactions" for 127.0.0.1 at 2013-07-22 16:05:52 +0200
Processing by TransactionsController#create as JS
  Parameters: {"utf8"=>"✓", "transaction"=>{"product_id"=>"239"}, "commit"=>"Acquisition"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'A0J7Ii29W7NPO0ECCccm6g' LIMIT 1
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? ORDER BY products.created_at DESC LIMIT 1  [["id", "239"]]
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 422 Unprocessable Entity in 12ms

トランザクションの作成を妨げる可能性があるものを知っていますか?

4

1 に答える 1

0

同様の問題に遭遇しました。CSRF認証トークンを送信していることを確認してください-レールを使用するform_tagか、自動的に含まれるものを使用しますが、手動のAJAXリクエストは送信しません。

于 2013-12-18T00:58:58.063 に答える