我正在使用Stripe和Ruby on Rails 3.2构建一个小概念证明.到目前为止,我已经看过如何在RoR应用程序中实现Stripe的Railscast,它的工作非常好.
我已经按照RailsCast#288 Billing with Stripe构建了我的应用程序.现在,我的用户可以添加和编辑他们的信用卡,甚至可以注册到课程,并在完成后通过信用卡收费.
现在我一直在使用Stripe的众多测试信用卡进行测试,我想在提出时捕获尽可能多的异常.我正在注册模型中使用Stripe的示例错误,如下所示:
class Registration < ActiveRecord::Base
belongs_to :user
belongs_to :session
attr_accessible :session_id, :user_id, :session, :user, :stripe_payment_id
validates :user_id, :uniqueness => {:scope => :session_id}
def save_with_payment(user, stripe_card_token)
if valid?
if user.stripe_customer_id.present?
charge = Stripe::Charge.create(
:customer => user.stripe_customer_id,
:amount => self.session.price.to_i * 100,
:description => "Registration for #{self.session.name} (Id:#{self.session.id})",
:currency => 'cad'
)
else
customer = Stripe::Customer.create(
:email => user.email,
:card => stripe_card_token,
:description => user.name
)
charge = …Run Code Online (Sandbox Code Playgroud) exception-handling ruby-on-rails ruby-on-rails-3 stripe-payments
我正在尝试添加FactoryGirl支持我的控制器中的默认脚手架规格,我似乎无法找到正确的语法.
这是一个示例测试:
describe "POST create" do
describe "with valid params" do
it "creates a new Course" do
expect {
post :create, {:course => valid_attributes}, valid_session
}.to change(Course, :count).by(1)
end
do
Run Code Online (Sandbox Code Playgroud)
我替换为:
describe "POST create" do
describe "with valid params" do
it "creates a new Course" do
expect {
post :create, course: FactoryGirl.build(:course)
}.to change(Course, :count).by(1)
end
do
Run Code Online (Sandbox Code Playgroud)
规格/工厂/ courses.rb
FactoryGirl.define do
factory :course do
association :provider
name "Course name"
description "course description"
end
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ course.rb
class Course < ActiveRecord::Base …Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby on Rails和Stripe构建一个小型支付处理模块以获得乐趣,我想知道这个方法(用于检查给定用户是否已经使用Stripe存档的卡)可以重构:
class User < ActiveRecord::Base
...
def has_card?
customer = Stripe::Customer.retrieve(self.stripe_customer_id)
if customer.cards.count > 0
true
else
false
end
end
end
Run Code Online (Sandbox Code Playgroud)
我认为这if句话看起来很傻但无法解释为什么(我白天不是开发人员,我只是涉猎)
我目前正在查看优秀的Rails-composer中的一段代码,我不明白第3行中的Embedded Ruby是做什么的:
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我看过Ruby文档并没有运气.一旦我理解了这段代码是如何工作的,我想扩展它以支持所有级别的flash[]消息.
我正在尝试在OS X 10.9上运行"捆绑更新",但它失败并出现以下错误消息:
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/FrancisO/.rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking for ruby/util.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/FrancisO/.rvm/rubies/ruby-1.9.3-p392/bin/ruby
/Users/FrancisO/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/FrancisO/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:506:in …Run Code Online (Sandbox Code Playgroud)