LoginSignup
2
4

More than 5 years have passed since last update.

Ruby on Rails [学習記録-第14章-] deviseの整理

Posted at

deviseとは

  • 簡単に言ってしまうと認証機能(ログイン/ログアウト)を作れるGem

deviseのインストール

  • Gemなので最初にGemファイルにgem 'devise'と記載する。
  • その後、bundle installコマンドを実行。
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

~中略~

gem 'devise'
terminal
$ bundle install
  • 次にdeviseのセットアップを行う。
  • devise専用のコマンドを利用して設定ファイルを作成する。
terminal
$ rails g devise:install
# deviseの設定ファイルを作成

rails g devise コマンド

terminal
$ rails g devise user
# deviseコマンドでモデルを作成
  • 以下のファイルが生成される。

    • app/models/user.rb
    • db/migrate/2014XXXXXXXXXX_devise_create_users.rb
    • test/fixtures/users.yml
    • test/models/user_test.rb
  • ルートファイルには以下の文章が自動的に追加される。

routes.rb
Rails.application.routes.draw do
  devise_for :users
#以下略

deviseで使える機能

  • deviseでは以下のようなメソッドが用意されている。
    • devise_for
    • user_sign_in?
    • configure_permitted_parameters
    • devise_error_message!
    • current_user

devise_for

  • devise_forはログインまわりに必要なルーティングを一気に生成してくれるdeviseのヘルパーメソッド。
  • devise_for :usersの記述により、以下のように「ログイン・新規登録」で必要なルーティングが生成される。
routes.rb
  Prefix Verb   URI Pattern                    Controller#Action
                    root GET    /                              tweets#index
              tweets_new POST   /tweets/new(.:format)          tweets#new
                  tweets POST   /tweets(.:format)              tweets#create
                         GET    /users/:user_id(.:format)      users#show
                         DELETE /tweets/:id(.:format)          tweets#destroy
                         GET    /tweets/:id/edit(.:format)     tweets#edit
                         PATCH  /tweets/:id(.:format)          tweets#update
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy

user_sign_in?

  • ユーザーがサインインしているかどうか検証するメソッド。
  • サインインしている場合にはtrueを返し、サインインしていない場合にはfalseを返す。
  • 下記のようにif文または、unless文とともに使用することが多い。
<% if user_signed_in? %>
  # ユーザーがサインインしている場合に実行する処理
<% end %>

configure_permitted_parameters

  • deviseでは初期状態でサインアップ時にメールアドレスとパスワードのみを受け取るようにストロングパラメーターが設定してあるので、追加したキーのパラメーターは許可されていない。
  • 追加のパラメーターを許可したい場合は、application_controllerにおいてbefore_actionにconfigure_permitted_parametersメソッドを設定する。
application_controller.rb
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:ストロングパラメーターを追加したいアクション名, keys: [:追加するキー])
  end
end

devise_error_messages!

  • ログイン関係のエラーを表示させるコード。
<%= devise_error_messages! %>

current_user

  • 現在ログイン中のユーザーのレコードを、userクラスのインスタンスとして取得することができるメソッド。
<%= current_user.name %>
2
4
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
2
4