4-3加入admin复盘

  1. git checkout -b story2
  2. 在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
  3. 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: "[email protected]", password: "123456", password_confirmation: "123456")
u.save
u.is_admin = true
u.save
7.seeds.rb
u = User.new
u.email = "[email protected]"
u.password = "123456"
u.password_confirmation = "123456"
u.is_admin = true
u.save
8.layout
layout "admin"
touch app/views/layouts/admin.html.erb

你可能感兴趣的:(4-3加入admin复盘)