【问题标题】:Validation function not working rails 4验证功能不起作用 rails 4
【发布时间】:2014-05-16 18:33:29
【问题描述】:

我正在使用设计用户模型(使用 rails 4)。这是我当前的用户模型。我想要的是一个不允许 :super_admin 在 :admin 为假的同时为真的验证。这是模型的当前版本:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  validate :if_super_admin_then_must_be_admin

  def if_super_admin_then_must_be_admin
    if :super_admin && !:admin
      errors.add(:admin, "can't be false if :super_admin is true.")
    end
  end
end

我的 rspec 测试失败,如果 :super_admin 为 true 且 :admin 为 false,则模型有效。我使用控制台仔细检查了它,所以问题不在规范中。

【问题讨论】:

    标签: ruby-on-rails validation ruby-on-rails-4 rspec devise


    【解决方案1】:

    已修复:

    def if_super_admin_then_must_be_admin
      if self.super_admin? && !self.admin?
        errors.add(:admin, "can't be false if :super_admin is true.")
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2016-01-08
      • 2014-07-29
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多