Help us understand the problem. What is going on with this article?

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

--- 全体の流れ ---
その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
  • ちゃんとできましたか?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした