これまでに作った機能について簡単にまとめていく。
今回は、
deviseを使った管理画面の作成
前提条件
deviseを使ったログイン機能があること
まだの方はこちら
使用モデル
Userモデル(4については記事中で追加)
カラム | データ型 | |
---|---|---|
1 | name | string |
2 | string | |
3 | encrypted_password | string |
4 | admin | boolean |
adminカラムの追加
1 |
$ rails g migration add_admin_to_users admin:boolean |
1 2 3 4 5 |
class AddAdminToUsers < ActiveRecord::Migration[5.2] def change add_column :users, :admin, :boolean, default: false end end |
rails db:migrateの実行
Gemの導入
1 |
gem 'rails_admin', '~> 2.0' |
bundle installの実行
管理画面の作成
1 |
$ rails g rails_admin:install |
途中でエンターボタン
Gemの導入
1 |
gem 'cancancan' |
bundle installの実行
Abilityモデルの作成
1 |
$ rails g cancan:ability |
アクセス権限の設定
models/ability.rb
1 2 3 4 5 6 7 8 9 10 |
class Ability include CanCan::Ability def initialize(user) if user.try(:admin?) can :access, :rails_admin can :manage, :all end end end |
Devise、CancanCanの記述をコメントアウト
config/initializers/rails_admin.rb
1 2 3 4 5 6 7 8 |
## == Devise == config.authenticate_with do warden.authenticate! scope: :user end config.current_user_method(&:current_user) ## == CancanCan == config.authorize_with :cancancan |
以上で作成完了