管理员后台操作权限控制

1- controller/admin/jobs_controller.rb

before_filter :require_is_admin

def require_is_admin
if !current_user.admin?
flash[:alert] = 'You are not admin'
redirect_to root_path
end
end

2- models/user.rb

def admin?
is_admin
end

3- 运行 rails g migration add_is_admin_to_user

4- db/migrate/xxxxx_add_is_admin_to_user.rb

def change
add_column :users, :is_admin, :boolean, default: false
end

5- rake db:migrate

6- 后台控制台

rails console
u = User.first
u.is_admin = true
u.save
exit

Comments

comments powered by Disqus