我在应用程序中使用Desive进行身份验证.

我该如何禁止某些用户登录——比如禁用用户?

推荐答案

这样做:

User模型创建一个名为is_active的列.

然后将以下代码添加到User型号:

class User < ActiveRecord::Base
  #this method is called by devise to check for "active" state of the model
  def active_for_authentication?
    #remember to call the super
    #then put our own check to determine "active" state using 
    #our own "is_active" column
    super and self.is_active?
  end
end

UPDATE

正如马特·哈金斯(Matt Huggins)所指出的,这种方法现在被称为active_for_authentication?(Documentation)

Ruby-on-rails相关问答推荐

Rails HotWire和View Components:涡轮框架不会取代内容

方法和括号之间没有空格时出错

Rails Heroku部署:预编译assets资源 失败.SassC::语法错误

Data-turbo-stream=&FALSE&QOOT;不适用于带有方法POST的表单

包含字母和数字组合的正则表达式

Rails 5.1.7:Sprockets::Rails::Helper::AssetNotFound

为什么是 json.partial!使用新的哈希语法传递本地时,jbuiler 模板中的行不执行?

您如何测试方法调用块内调用的方法,以及使用 rspec 在块内传递给该方法调用的内容

升级到 Rails 6.1.6.1 导致 Psych::DisallowedClass: 试图加载未指定的类:符号

如何使用 RSpec 和 Devise/CanCan 进行集成测试?

如何使用 Capistrano gem 为生产数据库 seeder ?

使用回形针进行简单裁剪

在 Rails 中,如何处理多个选中的复选框,只需拆分 , 或?

是否可以在 Rails 集成测试或规范中指定用户代理?

在 Rails 中将 DateTime 字符串转换为 UTC

Elastic Beanstalk Ruby/Rails 需要安装 git,所以 bundle install 可以工作.. 但不是

ActiveRecord 查找现有表索引

ActiveRecord 事务中的错误处理?

# 轨道中的 TODO

如何在不触及 updated_at 属性的情况下更新单个属性?