限制用户所在列的行数我正在构建一个呼叫跟踪应用程序,作为学习rails和twilio的一种方式。根据参数
现在,我有模型方案计划has_many用户has_many手机。
在计划模型中,我有一个名为max_phone_numbers的参数。
我想要做的就是根据计划给出的max_phone_numbers限制用户拥有的电话数量。
流程看起来是这样的:
1)用户购买了一堆电话号码 2)的当User.phones.count = max_phone号码,然后购买多个电话号码能力被禁止,并且链接弹出到upgrade_path
我不太清楚我会如何去做这件事。我需要在我的模型和我的控制器中做什么组合?
我会在我的控制器中定义什么,以这种方式在视图中,如果/然后围绕按钮声明,我可以扭曲?
即如果达到极限,比证明这一点,否则显示按钮
我把我的模型是什么,以防止刚访问的链接,而不是别人?
任何指导,或者在做这样的事情的资源,将不胜感激
这里是我当前的用户模型
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# password_digest :string(255)
# remember_token :string(255)
# twilio_account_sid :string(255)
# twilio_auth_token :string(255)
# plan_id :integer
# stripe_customer_token :string(255)
#
# Twilio authentication credentials
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :plan_id, :stripe_card_token
has_secure_password
belongs_to :plan
has_many :phones, dependent: :destroy
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: true
validates :password, presence: true, length: { minimum: 6 }, on: :create
validates :password_confirmation, presence: true, on: :create
validates_presence_of :plan_id
attr_accessor :stripe_card_token
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
def create_twilio_subaccount
@client = Twilio::REST::Client.new(TWILIO_PARENT_ACCOUNT_SID, TWILIO_PARENT_ACCOUNT_TOKEN)
@subaccount = @client.accounts.create({:FriendlyName => self[:email]})
self.twilio_account_sid = @subaccount.sid
self.twilio_auth_token = @subaccount.auth_token
save!
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
Zeikt,这就是天才。非常感谢帮助解构这个。我会在今天或明天实施它,让你知道它是如何发展的。 –