LoginSignup
27
27

More than 5 years have passed since last update.

RailsAdminセットアップ on Rails4 and Heroku

Posted at
ruby -v
----------
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]
----------
rails -v
----------
Rails 4.0.2
----------

Gemインストール

Gemfile

# Admin Page
gem 'rails_admin'
gem 'devise'
bundle install

devise関連

  • Deviseインストール
rails g devise:install
  • 管理者モデル作成
rails g devise admin_user
  • models/admin_user.rb編集

:registerable:recoverableをコメントアウト

class AdminUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,
         # :registerable, :recoverable,
         :rememberable, :trackable, :validatable
end
  • db/migrate/20131209044829_create_rails_admin_histories_table.rb
class DeviseCreateAdminUsers < ActiveRecord::Migration
  def change
    create_table(:admin_users) do |t|
      ## Database authenticatable
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""

      ## Recoverable
#      t.string   :reset_password_token
#      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0, :null => false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps
    end

    add_index :admin_users, :email,                :unique => true
#    add_index :admin_users, :reset_password_token, :unique => true
    # add_index :admin_users, :confirmation_token,   :unique => true
    # add_index :admin_users, :unlock_token,         :unique => true
  end
end
  • マイグレーション
rake db:migrate

RailsAdminインストール

  • RailsAdminインストール
rails g rails_admin:install
----------
           -  Hello, RailsAdmin installer will help you set things up!
           -  I need to work with Devise, let's look at a few things first:
           -  Checking for a current installation of devise...
           -  Found it!
           -  Looks like you've already installed it, good!
           ?  Where do you want to mount rails_admin? Press <enter> for [admin] > 
        gsub  config/routes.rb
       route  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
           -  And you already set it up, good! We just need to know about your user model name...
           -  We found 'admin_user' (should be one of 'user', 'admin', etc.)
           ?  Correct Devise model name if needed. Press <enter> for [admin_user] > 
           -  Ok, Devise looks already set up with user model name 'admin_user':
           -  Now you'll need an initializer...
      create  config/initializers/rails_admin.rb
           -  Adding a migration...
      create  db/migrate/20131209044829_create_rails_admin_histories_table.rb
           -  Job's done: migrate, start your server and visit '/admin'!
----------
  • マイグレーション
rake db:migrate
  • 管理者作成
rails c

32桁の16進数乱数生成

SecureRandom.hex(16)
=> "5883b9210d62398808920f7cc5183029"
AdminUser.create( email: "xxxxxxxxxx@gmail.com", password: "5883b9210d62398808920f7cc5183029", password_confirmation: "5883b9210d62398808920f7cc5183029" )
  • routes.rbのmountとdevise_forの順序を入れ替える

※これを入れ替えないとリダイレクトループ発生

MyApp::Application.routes.draw do
  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
  devise_for :admin_users
  
end

MyApp::Application.routes.draw do
  devise_for :admin_users
  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
  
end

ja.yml追加

  • devise
curl https://gist.github.com/yhara/606476/raw/15605bdbc28067c7983cefae3ae12105ee93c243/devise.ja.yml >> config/locales/ja.yml
----------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3573    0  3573    0     0   1991      0 --:--:--  0:00:01 --:--:--  6076
----------

↓下記の「ja:」部分を削除

ja:
  errors:
  • RailsAdmin

Japanese translation for RailsAdmin

curl https://gist.github.com/mshibuya/1662352/raw/a5ce6fb646d53ca44434a8b7ab238aeeb8791d27/rails_admin.ja.yml >> config/locales/ja.yml
----------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4937    0  4937    0     0   6426      0 --:--:-- --:--:-- --:--:--  8424
----------

↓下記の「ja:」部分を削除

config/locales/ja.yml

ja:
  admin:

ローカルサーバ起動

rails s

Admin Page - http://0.0.0.0:3000/admin

Heroku

git add . && git commit -m "RailsAdmin導入"
git push heroku master && heroku run rake db:migrate

※heroku上でのrails cの起動時間分の課金を防ぐ為に一旦Webをストップ

heroku ps:scale web=0 worker=0
heroku run rails c
SecureRandom.hex(16)
=> "5883b9210d62398808920f7cc5183029"

AdminUser.create( email: "xxxxxxxxxx@gmail.com", password: "5883b9210d62398808920f7cc5183029", password_confirmation: "5883b9210d62398808920f7cc5183029")

exit
heroku ps:scale web=1 worker=0
heroku open

/adminへアクセス

27
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
27
27