我创建了一个 ajax 按钮来创建事务,但是当我单击它时,Transaction.count
它不会增加。
我有三个模型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
您知道什么会干扰交易的创建吗?