我想把小数点从价格中去掉,然后再把价格发送给表格/条形。有什么不同的方法可以用来做这件事吗?
在我的product.rb模型中,我有:
def monetize_amount
self.price.to_s.delete(".").to_f
end在charges_controller中有:
def create
@product = Product.find(params[:id])
# Amount in cents
**@amount = @product.monetize_amount**
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
: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发布于 2014-12-17 05:41:45
就像argentum47说的那样。将其转换为整数值,单位为美分。
class Product < AR::Base
def price_in_cents
(price * 100).to_i
end
end然后你以后可以做:
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @product.price_in_cents,
#…https://stackoverflow.com/questions/27518358
复制相似问题