LoginSignup
0
0

More than 1 year has passed since last update.

Rails Deviseの導入

Last updated at Posted at 2021-06-09

Gemfileに追記

gem 'devise'

いつも通りの

bundle install

※ターミナルに

Using devise 4.8.0

が出力されればOK

ローカルサーバーの再起動

rails g devise:install

これを実行して

Running via Spring preloader in process 78357
      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================

Depending on your application's configuration some manual setup may be required:

  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.

     * Required for all applications. *

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

       root to: "home#index"

     * Not required for API-only Applications *

  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>

     * Not required for API-only Applications *

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

       rails g devise:views

     * Not required *

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

これが出力されればOK

モデルの作成

rails g devise user
config/routes.rb
Rails.application.routes.draw do
  devise_for :users

devise_forの記述が自動で追加されていたらOK。
マイグレーションファイルの作成。

db/migrate/20XXXXXXXXX_devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration[6.0]
 def change
   create_table :users do |t|
     ## Database authenticatable
     t.string :email,              null: false, default: ""
     t.string :encrypted_password, null: false, default: ""

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

     ## Rememberable
     t.datetime :remember_created_at

     # 省略

     t.timestamps null: false
   end

   add_index :users, :email,                unique: true
   add_index :users, :reset_password_token, unique: true
   # add_index :users, :confirmation_token,   unique: true
   # add_index :users, :unlock_token,         unique: true
 end
end

他のモデルの作成と同じく、マイグレーションファイルを作成して、Usersテーブルに欲しいカラムと型を追記。

追記は

## Database authenticatable

この下に記述。
emailとpasswordはデフォルトで設定されているので、その前後に欲しいカラムを追加。

マイグレーション実行

rails db:migrate

これでテーブルの作成が完了。
※Sequel Proなどで視覚的にも確認しましょう。

ビューの作成。
注意点はdeviseを使用する場合は、専用のコマンドで作成しなければ、意味がないということ。
※モデルやコントローラーでも同じだが。

rails g devise:views

ここまで出来たら、あとはビューを調整するだけ。
デフォルトでビューが作成されているので、

http://localhost:3000/users/sign_up
で新規登録画面
http://localhost:3000/users/sign_in
でログイン画面が確認できます。

新規登録のビューは
app/views/devise/registrations
のnew.html/erb

ログイン画面のビューは
app/views/devise/sessions
のnew.html.erb

に格納されています。

ビューを調整しても、追加したカラムがある場合は登録ボタンを押したらエラーが出ます。

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  private
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:追加したカラム,:追加したカラム2])
  end
end
0
0
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
0
0