125
165

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 3 years have passed since last update.

Deviseの設定手順をまとめてみた。 その2 ViewとControllerのカスタマイズ編

Last updated at Posted at 2015-04-26

--- 全体の流れ ---
その1 導入編
その2 ViewとControllerのカスタマイズ編 ←今ココ
その3 Deviseの日本語化編
その4 ユーザーIDで、ログイン認証編

実運用では、カスタマイズが必須だよねーってことで、
今回は、ログイン画面やサインイン画面をカスタマイズしよーって話。

1. deviseのViewをカスタマイズ

  • 前回、ユーザー管理用のテーブルとして、usersテーブルを作ってたので、以下のコマンドを実行
$ rails g devise:views users
      invoke  Devise::Generators::SharedViewsGenerator
      create    app/views/users/shared
      create    app/views/users/shared/_links.html.erb
      invoke  form_for
      create    app/views/users/confirmations
      create    app/views/users/confirmations/new.html.erb
      create    app/views/users/passwords
      create    app/views/users/passwords/edit.html.erb
      create    app/views/users/passwords/new.html.erb
      create    app/views/users/registrations
      create    app/views/users/registrations/edit.html.erb
      create    app/views/users/registrations/new.html.erb
      create    app/views/users/sessions
      create    app/views/users/sessions/new.html.erb
      create    app/views/users/unlocks
      create    app/views/users/unlocks/new.html.erb
      invoke  erb
      create    app/views/users/mailer
      create    app/views/users/mailer/confirmation_instructions.html.erb
      create    app/views/users/mailer/reset_password_instructions.html.erb
      create    app/views/users/mailer/unlock_instructions.html.erb

2. deviseのControllerをカスタマイズ

  • ここも、usersテーブルに合わせて、コントローラーを作成
$ rails g devise:controllers users
      create  app/controllers/users/confirmations_controller.rb
      create  app/controllers/users/passwords_controller.rb
      create  app/controllers/users/registrations_controller.rb
      create  app/controllers/users/sessions_controller.rb
      create  app/controllers/users/unlocks_controller.rb
      create  app/controllers/users/omniauth_callbacks_controller.rb
====================================================================

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

  Ensure you have overridden routes for generated controllers in your route.rb.
  For example:

    Rails.application.routes.draw do
      devise_for :users, controllers: {
        sessions: 'sessions'
      }
    end

====================================================================
  • 使用するコントローラーで、コメントになっているところを外す

  • ざっくり、よく使うControllerについて説明すると...

    1. confirmations_controller.rb: ユーザー登録確認メール内のリンクを押下時
    2. passwords_controller.rb: パスワードを忘れた方は。。。の場合
    3. registrations_controller.rb:ユーザー登録時
    4. sessions_controller.rb : ログイン時

3. ルーティング

  • 実は、ここがキモ!!

  • config/routes.rbに、以下の内容を記述

  devise_for :users, :controllers => {
    :registrations => 'users/registrations',
    :sessions => 'users/sessions'   
  } 
    
  devise_scope :user do
    get "sign_in", :to => "users/sessions#new"
    get "sign_out", :to => "users/sessions#destroy" 
  end

4. Railsを再起動して、確認。

$ rails s
  • ちゃんとできましたか?
125
165
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
125
165

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?