【问题标题】:how to deactivate and activate users from application in ruby on rails如何在 ruby​​ on rails 中从应用程序停用和激活用户
【发布时间】:2013-03-22 12:44:48
【问题描述】:

我正在我的 ruby​​ on rails 应用程序中实现仪表板工具,其中只有管理员可以进入并查看在应用程序中注册的所有用户。从仪表板,管理员可以停用或激活用户(可能是一段时间)。我有包含 is_active 列的用户表。我知道,可以通过在表格的这一列上设置标志来完成。能否请您详细解释一下。

以下是我的用户表。

create_table "users", :force => true do |t|
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.boolean  "is_admin"
    t.boolean  "is_active"
    t.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
    t.string   "username"
  end

请帮助我从应用程序中实现用户的激活和停用(管理员只能这样做)

【问题讨论】:

    标签: ruby-on-rails admin dashboard


    【解决方案1】:

    您可以在User 模型中创建两个方法来激活和停用用户

    class User < ActiveRecord::Base
    
     def activate_account!
       update_attribute :is_active, true
     end
    
     def deactivate_account!
       update_attribute :is_active, false
     end
    end
    

    由于is_active 列的类型是布尔型,您可以使用rails 生成的user.is_active? 方法来检查用户的帐户是否处于活动状态

    【讨论】:

      【解决方案2】:

      对于您,我建议您研究 CanCan 或其他授权宝石。您提供的迁移中对此无能为力,因此我建议您查看有关实施 cancan 的截屏视频。

      http://railscasts.com/episodes/192-authorization-with-cancan

      【讨论】:

      • 感谢您的建议。我有仪表板页面,我在其中放置了复选框控件以激活或停用用户。我想如果我点击该复选框,用户应该在一段时间内被停用。仅使用设计是不可能的。请让我知道,如果可能,请解释一下。谢谢
      • Devise 提供身份验证。这让您知道谁在使用您的应用程序。 CanCan 提供了授权,这指定了用户可以读取或写入应用程序的哪些部分。
      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 2011-11-26
      • 2014-11-05
      • 1970-01-01
      • 2010-10-22
      • 2014-01-07
      • 1970-01-01
      相关资源
      最近更新 更多