https://github.com/sferik/rails_admin
安裝步驟
- On your gemfile:
gem 'rails_admin', '~> 1.0' #
增加gem至gemfile - Run
bundle install
- Run
rails g rails_admin:install
- Provide a namespace for the routes when asked
- Start a server
rails s
and administer your data at /admin. (if you chose default namespace: /admin)
在使用者增加欄位 判斷是否為管理者 admin:boolean
增加欄位步驟
rails generate migration add_admin_to_users admin:boolean
➜ eat git:(master) ✗ rails c
Running via Spring preloader in process 31182
Loading development environment (Rails 5.0.0.1)
irb(main):001:0> u=User.last
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<User id: 3, email: "1@gmail.com", created_at: "2016-10-04 03:01:32", updated_at: "2016-10-04 03:01:32", admin: nil>
irb(main):002:0> u.admin=true
=> true
irb(main):003:0> u.save
(0.6ms) begin transaction
SQL (0.4ms) UPDATE "users" SET "updated_at" = ?, "admin" = ? WHERE "users"."id" = ? [["updated_at", 2016-10-04 03:20:08 UTC], ["admin", true], ["id", 3]]
(1.8ms) commit transaction
=> true
irb(main):004:0>
增加判斷式
config > initializers > rails_admin.rb
config.authorize_with do
redirect_to main_app.root_path unless current_user.admin==true
end
沒有留言:
張貼留言