【发布时间】:2018-12-29 07:02:37
【问题描述】:
我有一家商店,您可以在其中通过下载购买数字产品。购买的每件产品(我测试过多种产品)在购买后立即在感谢页面上显示相同的产品信息(标题、art_link 和 download_url)。
我在这里做错了什么,导致它无法从刚购买的包中加载正确的数据?
我正在使用 Stripe 进行付款。其他一切正常,钱通过,电子邮件发送和收据链接,UUID 工作等等。
更新:我检查了我的数据库,一切都正确保存。唯一的问题是 purchases/show 视图无法正确呈现。
购买/展示:
<p><b><%= @pack.title %></b></p>
<%= image_tag("#{@pack.art_link}", :alt => "#{@pack.title} sound library.", :width => 330, :height => 330, class: "img-center img-responsive shade") %>
<p><a class="btn btn-success top-drop" href="<%= @pack.download_url %>" target="_blank">Download Files</a></p>
purchases_controller:
def show
@purchase = Purchase.find_by_uuid(params[:id])
@pack = Pack.find(@purchase.product_id)
set_meta_tags noindex: true
end
购买模式:
attr_accessor :download_token
after_create :email_purchaser
def to_param
uuid
end
def email_purchaser
PurchaseMailer.purchase_receipt(self).deliver
end
def Purchase.new_token
SecureRandom.urlsafe_base64
end
def create_download
self.download_token = Purchase.email.new_token
update_attribute(:download, Purchase.email(download_token))
update_attribute(:download_sent_at, Time.zone.now)
end
charges_controller:
def create
pack = Pack.find(params[:product_id])
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken],
)
# Amount in cents
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => pack.price_in_cents,
:description => 'Rails Stripe customer',
:currency => 'usd',
)
purchase = Purchase.create(
email: params[:stripeEmail],
card: params[:stripeToken],
amount: pack.price_in_cents,
description: charge.description,
currency: charge.currency,
customer_id: customer.id,
product_id: pack.id,
uuid: SecureRandom.uuid,
)
redirect_to purchase
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
非常感谢,这真的让我卡住了。
Routes.rb:
resources :packs, :path => 'products'
resources :charges
resources :purchases, only: [:show]
【问题讨论】:
-
您是否检查过您是否每次都加载不同的购买(
PurchasesController中的不同params[:id])?另外,您可以在您的帖子中添加routes.rb(仅与购买相关的部分)吗? -
@MrShemek 嘿,谢谢。对于我测试的 3 次不同的购买,我看到一个不同的 URL (UUID),所以我认为这将是一个不同的 id。不过我不太确定,你是不是还有别的意思,我该如何检查?我已经用路线更新了我的问题。
-
我对Rails一点也不熟悉,但是当你
redirect_to purchase时,看起来purchases_controller正在使用ID参数来查找产品。但是我看不到那个 ID 是如何通过的,你需要explicitly set it 吗?即当你从charges_controller重定向时使用purchase.product_id -
@karllekko 不需要 (source)。 @Jake - 我检查了类似的解决方案,在我的情况下一切正常。我认为检查的唯一选择是将
binding.pry放入控制器和视图中,并检查加载为@pack的对象。 -
@Jake 让我们开始聊天;会更快
标签: ruby-on-rails ruby stripe-payments