我有一个应用程序,其中包含用户模型,图钉模型和条带付款的收费控制器。当我作为开发中的用户登录并尝试通过条带支付购买引脚时,我收到以下错误:
ChargesController中的ActiveRecord :: RecordNotFound #create
找不到没有ID的Pin
它表示错误来自下面代码中的@pin = Pin.find(params [:id])。我相信它是这样做的,因为引脚仍然是'零。我不确定是否或如何为其分配值,以便它对应于与该引脚相关的确切价格。
class ChargesController < ApplicationController
def create
# Amount in cents
@pin = Pin.find(params[:id])
@amount = (@pin.price * 100).floor
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
end
此外,无论按钮或链接调用ChargesController #create方法都可能在下面的代码show.html.erb:
应用程序/视图/针/显示/ html.erb
<% if @pin.user == current_user %>
<%= form_tag charges_path, id: 'chargesForm' do %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="btn-buy" type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-heart"></span> I want this!</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
token: function(token, arg) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargesForm").submit();
}
});
document.getElementById('btn-buy').addEventListener('click', function(e) {
handler.open({
name: <%= @pin.manufacturer %>',
description: '<%= @pin.description %>',
amount: '<%= (@pin.price * 100).floor %>'
});
e.preventDefault();
})
</script>
<% end %>
<% else %>
<%= link_to 'I want this!', new_user_registration_path, class: "btn btn-success btn-lg btn-block" %>
<% end %>
<ul id="details-infobox" class="list-group">
<li class="list-group-item active clearfix">DETAILS</li>
<li class="list-group-item">
<p><strong>Description:</strong> <%= @pin.description %></p></li>
<li class="list-group-item clearfix">
<span class="pull-left content-qualifier"><b>Price: $ <%= @pin.price %></b></span>
</li>
</ul>
</aside>
</section>
我该怎么做才能解决这个问题?谢谢!