Rails 应用程序中 Braintree 类似“购物车”的实现/查找以前的 params[:id]?
我正在尝试将 Braintree 集成到我的 Rails 应用程序中,该应用程序已经有存款控制器、模型和视图。现在基本上您可以指定金额,但我将其状态设置为“待处理”。我希望用户可以存入这样的押金,然后随时使用 Braintree(购物车)付款。我是否必须创建另一个控制器和/或模型才能做到这一点? (例如,我见过的所有 Braintree 示例都希望立即付款)。
具体来说,我一直在尝试使用我已有的“存款”。我将用户姓名、信用卡信息等表格放在存款“显示”页面和确认按钮上。如果所有字段都满足验证,这似乎工作正常,但是当出现错误并再次呈现显示页面时,它就不行了。 在 DepositsController.rb 中:
def confirm
@deposit = Deposit.find(params[:id])
@result = Braintree::TransparentRedirect.confirm(request.query_string)
if @result.success?
render :action => "confirm"
else
render :action => "show"
end
end
问题是 :id
现在是 Braintree 交易 ID,而不是存款 id(主键)。所以当然找不到 Deposit.find(params[:id])
。 实现这一点的最佳方法是什么?我应该以某种方式存储以前的 id 还是以其他方式获取它?我应该使用另一个控制器吗?谢谢!
I'm trying to integrate Braintree into my rails app which already has a deposits controller, model, and view. Right now basically you can specify an amount but I have its status set to "Pending". I would like it so that the user can make such a deposit but then pay for it at any time using Braintree (ala shopping cart). Would I have to create another controller and/or model to do this? (For example all the Braintree examples I've seen want the payment immediately).
Specifically, I've been trying to just work with the 'deposits' I already have. I put the form for the user's name, credit card info, etc. on the deposits "show" page and a confirm button. This seems to work fine if all fields satisfy validation, however it doesn't when there is an error and renders the show page again.
In DepositsController.rb:
def confirm
@deposit = Deposit.find(params[:id])
@result = Braintree::TransparentRedirect.confirm(request.query_string)
if @result.success?
render :action => "confirm"
else
render :action => "show"
end
end
The problem is that :id
now is the Braintree transaction ID, rather than the deposits id (primary key). So of course Deposit.find(params[:id])
can't be found.
What is the best way to implement this? Should I store the previous id somehow or get it another way? Should I be using another controller? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是,您应该使用购物车模型,连接到我在这里收集到的存款模型。然而,根据其他问题,这种感觉可能会改变。
因此,仅根据您上面所写的内容:
如果我们遵循 RESTful 方法,您应该为所有新存款创建一个
Deposit#new
。在
Deposit#create
中,您可以将所有逻辑放入deposit.rb
模型文件中。这个逻辑包括,去布伦特里等等。您说您正在使用已有的存款,在这种情况下,它们应该在
Deposit#edit
方法中处理。在这方面我想问您更多问题,您使用 ActiveMerchant 吗?如果没有,为什么不呢?
Short answer is you should be using a Cart model, connected to this Deposit model from what i can gather here. Based on other questions, however, that feeling could change.
So, solely based on what you wrote above:
If we follow a RESTful approach, you should be creating a
Deposit#new
for all new deposits.In your
Deposit#create
, you would then put all of your logic into thedeposit.rb
model file. this logic includes, going to Braintree and such.You say you are working with the deposits you already have, in that case, they should be handled in the
Deposit#edit
method.Further questions I would ask of you in this regard, are you using ActiveMerchant? If not, why not?