我在学习RoR大约两个月了。
尝试使用https://monospace-rails.herokuapp.com/设置订阅网站,并希望将客户的实际地址字段添加到表单中。由于某种原因,我找不到任何关于此的信息。我可能正在寻找错误的关键词。
这是用户模型:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :stripe_token, :last_4_digits
attr_accessor :password, :stripe_token
before_save :encrypt_password
before_save :update_stripe
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :name
validates_presence_of :email
validates_uniqueness_of :email
validates_presence_of :last_4_digits
def stripe_description
"#{name}: #{email}"
end
def update_stripe
if stripe_id.nil?
if !stripe_token.present?
raise "We're doing something wrong -- this isn't supposed to happen"
end
customer = Stripe::Customer.create(
:email => email,
:description => stripe_description,
:card => stripe_token
)
self.last_4_digits = customer.active_card.last4
response = customer.update_subscription({:plan => "premium"})
else
customer = Stripe::Customer.retrieve(stripe_id)
if stripe_token.present?
customer.card = stripe_token
end
# in case they've changed
customer.email = email
customer.description = stripe_description
customer.save
self.last_4_digits = customer.active_card.last4
end
self.stripe_id = customer.id
self.stripe_token = nil
end
def self.authenticate(email, password)
user = self.find_by_email(email)
if user && BCrypt::Password.new(user.hashed_password) == password
user
else
nil
end
end
def encrypt_password
if password.present?
self.hashed_password = BCrypt::Password.create(password)
end
end
end
我假设我需要将地址字段添加到db,attr_accessible并在Stripe :: Customer.create中添加一些东西。我对如何编写它有点困惑,如果这一切都需要让它工作。
任何帮助甚至是正确方向的链接都是巨大的。