24
23

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.

Ruby on RailsAdvent Calendar 2013

Day 23

rails_admin で Railsの標準環境を作っておくとモックが捗るというお話。

Last updated at Posted at 2013-12-23

railsのセットアップは途中までだいたい同じ

最近rails4アプリを結構作るんですけど、admin周りまでのセットアップは毎回ほとんど同じだったので、そこまでの分をリポジトリとして公開してみました。役に立つかわからないけど、個人的には役に立っているのでw

以下に、作り方と公開リポジトリを記載しておきます。

環境

  • Debian 6.0.6
  • Ruby 2.0.0-p247 (rbenv)

RubyGems

  • rails 4.0.0
  • rails_admin 0.5.0
  • slim 2.0.0
  • slim-rails 2.0.1
  • bundler 1.3.5
  • rspec-rails 2.14.0
  • mysql2

rails setup

まずは、アプリの初期化

$ rails new app_name --skip-bundle
source 'https://rubygems.org'
ruby '2.0.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'

# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
gem 'mysql2'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

# Use slim as the Template engine
gem 'slim-rails'

# Use image upload plugin.
gem 'paperclip'

# Use markdown parse engine.
gem 'redcarpet'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the app server
gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
group :development, :test do
  # gem 'debugger', '1.6.0'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'rspec-rails'
  gem 'guard-rspec'
  gem 'factory_girl_rails'
  gem 'spring'
end

# User Production
group :production do
  gem 'pg'
end

# Use rail-admin as the Admin
gem 'rails_admin'
gem 'devise', '3.0.0.rc'
gem 'protected_attributes'

# Use pry-rails
gem 'pry-rails'

Gemfileは上記みたいな感じを用途に合わせてコメントアウトしていく。

$ bundle install

一旦インストール

$ rails s

起動するかチェック起動しない場合はだいたい環境問題なので対処する

rails_admin setup

ここからはrails_adminのセットアップ、本当は全部良しなにしてくれるけど、うまくいかないので自分でやる。

$ rails g devise:install
app/views/layouts/application.html.erb
<html>
<head>
  <title>SampleRailsAdmin</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
 
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
 
<%= yield %>
 
</body>
</html>

必要があればnoticeを追加

$ rails g devise admin_user
$ rake db:migrate
app/models/admin_user.rb
class AdminUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

直接書き込めるように一旦書き換える。

db/migrate/xxxxx_devise_create_admin_users.rb
class DeviseCreateAdminUsers < ActiveRecord::Migration
  def change
    create_table(:admin_users) do |t|
      ...

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

      ...
    end

    ...
    # add_index :admin_users, :reset_password_token, :unique => true
    ...
  end
end

必要部分をコメントアウト

$ rails g rails_admin:install

rails_admin 初期化

$ rake db:migrate

最後にマイグレート

$ rails c
> AdminUser.create(:email => "admin@test.com", :password => "hogehoge", :password_confirmation => "hogehoge")

本来はrails_adminが良しなにしてくれるのですが、rails4からうまくいかないみたいなので、初期ユーザーは自分で生成します。

$ rails s

http://localhost:3000/admin へアクセスしてチェック

無事表示されれば完了です。

スクリーンショット 2013-12-23 22.02.54.png

rails4admin

※よかったらforkなりして使ってください。

参考

24
23
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
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?