LoginSignup
24
27

More than 5 years have passed since last update.

Deviseインストール済環境でActiveAdminの導入

Last updated at Posted at 2015-01-26

前提

  • deviseがインストール済で、
  • ユーザと管理者は同じ Users テーブルで管理します。
  • roleは別テーブルで管理します。(cancancanを使用しない)

インストール方法抜粋:

# gemインストール
gem 'activeadmin'

# active admin初期設定生成(ユーザテーブルなど生成しないようにスキップオプション)
bundle exec rails g active_admin:install --skip-users

# 実行結果
create  config/initializers/active_admin.rb
      create  app/admin
      create  app/admin/dashboard.rb
       route  ActiveAdmin.routes(self)
    generate  active_admin:assets
      create  app/assets/javascripts/active_admin.js.coffee
      create  app/assets/stylesheets/active_admin.css.scss
      create  db/migrate/2015..._create_active_admin_comments.rb

  • config/intializers/active_admin.rbファイルの修正内容:

config.current_user_method = :current_admin_user

config.current_user_method = :current_user

config.logout_link_path = :destroy_admin_user_session_path

config.logout_link_path = :destroy_user_session_path
  • app/application_controller.rbに以下を追加:

def authenticate_admin_user!
  authenticate_user!

  # current_userはdevise提供のメソッドです。
  # 権限ユーザのroleについては、好きな方法でよいです。(自分の場合、has_roleメソッドで実装)
  unless current_user.has_role 'admin'
    flash[:alert] = "管理者用ページです。権限があるアカウントでログインしてください。"
    redirect_to root_path
  end
end
  • DB作成

 bundle exec rake db:migrate
  • サーバ起動確認
 bundle exec rails s
  • アクセス 下記のリンクを接続すると、ちゃんとユーザのログイン画面がでます。 localhost:3000/admin

参照リンク:
https://github.com/activeadmin/activeadmin/blob/master/docs/0-installation.md

24
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
27