RailsとTwilioを学習する方法として、Call-Trackingアプリケーションを構築しています。
今、私はモデルスキーム計画has_manyユーザーhas_many電話を持っています。
プランモデルには、max_phone_numbersというパラメーターがあります。
私がやりたいのは、プランが提供するmax_phone_numbersに基づいて、ユーザーが持つ電話の数を制限することです。
フローは次のようになります。
1)ユーザーが多数の電話番号を購入する2)User.phones.count = max_phone番号の場合、追加の電話番号を購入する機能が無効になり、upgrade_pathへのリンクがポップアップ表示されます
しかし、これをどのように行うのかはよくわかりません。モデルとコントローラーで実行する必要があることの組み合わせは何ですか?
ビューでボタンの周りのif/thenステートメントをワープできるように、コントローラーで何を定義しますか?
つまり、制限に達した場合は、これを表示するよりも、それ以外の場合はボタンを表示します
誰かが代わりにリンクにアクセスするのを防ぐために、モデルに何を入れますか?
このようなことを行うためのガイダンスやリソースをいただければ幸いです。
これが私の現在のユーザーモデルです
# == 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+\-.]+@[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