Vieenay Siingh
Vieenay Siingh

Reputation: 867

how to deactivate and activate users from application in ruby on rails

I am implementing dashboard facility in my ruby on rails application where only Admin can enter and see all users which are registered with application. From dashboard, admin can deactivate or activate users( may be for certain time of period). I have User table which has is_active column. I know , it can be done by setting flag on this column in table. Could you please explain it in details.

Following is my User table.

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

Please help me in implementing activation and deactivation of users from application(Admin can only this)

Upvotes: 0

Views: 1501

Answers (2)

Naveed
Naveed

Reputation: 11167

you can create two method in User model for activating & deactivating user

class User < ActiveRecord::Base

 def activate_account!
   update_attribute :is_active, true
 end

 def deactivate_account!
   update_attribute :is_active, false
 end
end

As type of is_active column is boolean, you can use user.is_active? method generated by rails to check if user's account is active or not

Upvotes: 1

Brian Petro
Brian Petro

Reputation: 1567

For you, I suggest looking into CanCan or another authorization gem. There isn't anything you can do in the migration, which you provided, about this so I suggest you check out this screen-cast about implementing cancan.

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

Upvotes: 0

Related Questions