3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails 5.2 (with --api) + ActiveAdmin + devise

Last updated at Posted at 2020-01-19

はじめに

Rails (with --api)でActiveAdminを使う際に必要となる手順についてまとめてあります。
他にも記事がたくさんありますが、deviseを使う使わないなどの選択肢で、記事通りで動かないことがあったので、ここではdeviseを使う前提で書いてあります。

環境

  • Rails 5.2.4
  • ActiveAdmin 2.6.0
  • devise 4.7.1
  • mini_racer 0.2.9

参考

https://blog.heroku.com/a-rock-solid-modern-web-stack
(ほぼリンクの手順ですが、config/application.rbの内容を補足)

Gem インストール

Gemfile
gem 'activeadmin', '2.6.0'
gem 'devise', '4.7.1'
gem 'mini_racer', '0.2.9'
$ bin/bundle install

ファイルの修正

app/controller/application_controller.rb
class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
end

次のファイルを継承してコントローラを実装する。

app/controller/api_controller.rb
class ApiController < ActionController::API
end
config/application.rb

require "sprockets/railtie"   # uncommented
...
  class Application < Rails::Application

  # Middleware for ActiveAdmin
    config.middleware.use Rack::MethodOverride
    config.middleware.use ActionDispatch::Flash
    config.middleware.use ActionDispatch::Cookies
    config.middleware.use ActionDispatch::Session::CookieStore

    config.generators do |g|
        g.scaffold_controller = :scaffold_controller
    end
  end
end
$ mkdir -p app/assets/config && echo '{}' > app/assets/config/manifest.js

インストール

$ bin/rails g active_admin:install
$ bin/rails db:migrate db:seed

動作確認

$ bin/rails s

モデルの追加方法

rails g active_admin:resource some_model
app/admin/some_model.rb
ActiveAdmin.register User do
  permit_params :xxx, :yyy, :zzz
end
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?