【发布时间】:2017-10-03 02:35:43
【问题描述】:
我正在使用 Stripe Checkout,我希望用户设置他/她想要支付的金额。我想将变量从“新”传递到“创建”控制器,特别是捐赠金额。如何使用 Stripe 模板将 @amount 变量从“new”传递到“create”。在我的控制器中,我有:
def new
@amount = 10
@amount_in_cents = @amount * 100
end
def create
# Amount in cents...HOW DO I GET THIS TO BE PULLED FROM 'NEW'
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => 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 new_charge_path
end
我对 new.html.erb 的看法:
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $<%= @amount %></span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="Your donation of $<%= @amount %>"
data-amount="<%= @amount_in_cents %>"
data-email = "test@gmail.com"
data-locale="auto"></script>
<% end %>
create.html.erb 的视图
<h2>Thanks, you paid <strong>$5.00</strong>!</h2>
<%= @amount %>
【问题讨论】:
-
有什么问题?您已经将它从
#new传递到您的视图和#create。
标签: ruby-on-rails stripe-payments