【问题标题】:Limit the number of rows of a column a user has based on parameter根据参数限制用户拥有的列的行数
【发布时间】:2012-10-08 14:28:31
【问题描述】:

我正在构建一个呼叫跟踪应用程序作为学习 rails 和 twilio 的一种方式。

现在,我有模型方案计划 has_many users has_many 手机。

在计划模型中,我有一个名为 max_phone_numbers 的参数。

我想做的是根据计划提供的 max_phone_numbers 限制用户拥有的电话数量。

流程看起来像这样:

1) 用户购买了一堆电话号码 2)当User.phones.count = max_phone numbers时,购买更多电话号码的功能被禁用,并弹出一个链接到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

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您可以向您的电话模型添加自定义验证,以检查用户是否已达到其限制。如果用户已达到限制,这将阻止创建任何新手机。

    在您的用户类中

    def at_max_phone_limit?
      self.phones.count >= self.plan.max_phone_numbers
    end
    

    在你的电话类中

    validate :check_phone_limit, :on => :create
    
    def check_phone_limit
      if User.find(self.user_id).at_max_phone_limit?
        self.errors[:base] << "Cannot add any more phones"
      end
    end
    

    在你的视图/表单中,你会做这样的事情

    <% if @user.at_max_phone_limit? %>
      <%= link_to "Upgrade your Plan", upgrade_plan_path %>
    <% else %>
      # Render form/widget/control for adding a phone number
    <% end %>
    

    【讨论】:

    • Zeikt,这是天才。非常感谢你帮助解构这个。我会在今天或明天实施,然后告诉你进展如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2011-07-09
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多