トランザクションを作成する 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
トランザクションの作成を妨げる可能性があるものを知っていますか?