一
app/controller/admin/ⅩⅩ_controller.rb
before_action :require_is_admin
def require_is_admin
if !current_user.admin?
flash[:alert] = 'You are not admin'
redirect_to root_path
end
end
二
app/models/user.rb
def admin?
is_admin
end
三
rails g migration add_is_admin_to_user
db/migrate/xxxxx.rb
def change
add_column :users, :is_admin, :boolean, default: false
end
rake db:migrate
因为 require_is_admin 将来很多地方会用到,
所以可以将app/controller/admin/ⅩⅩ_controller.rb下的require_is_admin方法重构到app/controllers/application_controller.rb下
网友评论