LoginSignup
6
9

More than 5 years have passed since last update.

gem deviseの使いかた

Posted at

はじめに

deviseとは、認証系の機能を簡単に追加できるgem

  • できること
    • ユーザー登録
    • メールを送信し、本登録
    • パスワードの再設定
    • ログインミスでアカウントロック など

1.gemの追加

コマンド

gem 'devise'

2.deviseのインストール

コマンド

rail g devise:install

実行結果

      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================

Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

       config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

     In production, :host should be set to the actual host of your application.

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>

  4. If you are deploying on Heroku with Rails 3.2 only, you may want to set:

       config.assets.initialize_on_precompile = false

     On config/application.rb forcing your application to not access the DB
     or load models when precompiling your assets.

  5. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

===============================================================================

3.deviseの設定

デフォルトのURLの指定

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }をdevelopmetn.rbに追加

config/envieronments/development.rb
Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  (省略)...

  # mailer setting
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end

root_urlの指定

デフォルトのURLにアクセスしたときに表示されるページを指定します。

config/route.rb
root 'sample#index'

flashメッセージの設定

applicatin.html.haml
<body>
  <p class="notice"><%= notice %></p>
  <p class="alert"><%= alert %></p>

  <%= yield %>

</body> 

deviseのviewを生成

devise関連のview(ログイン、サインアップ画面等)のデザインを変更するため

rails g devise:views

モデルの設定

モデルの生成

rails g devise user

マイグレーションファイルの編集

追加したいカラムがある場合はマイグレーションファイルを編集する。

マイグレーションの実行

コマンド

rake db:migrate

デフォルト以外のデータも保存できるようにする。

deviseはデフォルトのままでは、保存できるデータが決まっている。
ユーザー名やプロフィールなどはこのままでは保存できない。

Viewファイルを編集する

rails g devise:viewsで生成したファイルを必要に応じて編集する。

deviseのコントローラーを生成する

rails g devise:controllers users(scopes名)

ルーティングを設定する

生成したdeviseControllerのルーティングを設定します。

routes.rb
devise_for :users, :controllers => {
 registrations: 'users/registrations', 
 account_update:'users/registrations'
}

ストロングパラメータを設定する

コメントアウトを外して、必要なパラメータを追加しましょう。

controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_sign_up_params, only: [:create]
before_filter :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  def new
    super
  end

  # POST /resource
  def create
    super
  end

  # GET /resource/edit
  def edit
    super
  end

  # PUT /resource
  def update
    super
  end

   protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.for(:sign_up) << [:name, :image]
  end

  # If you have extra params to permit, append them to the sanitizer.
  def configure_account_update_params
    devise_parameter_sanitizer.for(:account_update) << [:name, :image]
  end
 end

deviseで出力される文字列の日本語化

ja.ymlを作成する

https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/ja.yml
からja.ymlを/config/localesに入れる。

devise.ja.ymlを作成する

https://gist.github.com/satour/6c15f27211fdc0de58b4
からdevise.ja.ymlを/config/localesに入れる。

日本語化を反映させる

config/application.rb
# config.i18n.default_locale = :de のコメントアウトがあるのでこれを利用すればよい
config.i18n.default_locale = :ja

さいごに

参考

(日本語化)
http://ruby-rails.hatenadiary.com/entry/20140804/1407168000
http://qiita.com/shizuma/items/a52fd0ef5b60d61fa330#_reference-d3045741d561914d596e

(コントローラーカスタマイズ)
http://qiita.com/akasakas/items/138c29fa2cecd271cfe4

6
9
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
6
9