- git checkout -b story2
- 在admin/controller加入
before_action :authenticate_user!
before_action :admin_required
3.application_controller.rb
加入:
def admin_required
if !current_user.admin?
redirect_to "/", alert: "You are not admin."
end
end
4.user.rb
def admin?
is_admin
end - rails g migration add_is_admin_to_user
加入:
add_column :users, :is_admin, :boolean, default: false
rake db:migrate
rails c
u = User.new(email: "admin@test.com", password: "123456", password_confirmation: "123456")
u.save
u.is_admin = true
u.save
7.seeds.rb
u = User.new
u.email = "admin@test.com"
u.password = "123456"
u.password_confirmation = "123456"
u.is_admin = true
u.save
8.layout
layout "admin"
touch app/views/layouts/admin.html.erb