0
0

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導入手順】インストールと設定方法

Last updated at Posted at 2021-06-30

Railsで簡単にログイン機能を実装できる devise の導入手順をまとめてみました。
この手順通りに設定を進めれば、問題なく実装できるはずです!

Gemfileの一番下に gem 'devise' を記述

まずはGemfileに記述します
gem 'devise'

ターミナルで bundle install を実行

Gemfileに記述 + bundle install をすることで Gemfile.lock に追加されます
$ bundle install

ターミナルで rails g devise:install を実行

これで devise 関連のディレクトリやファイルが追加されます
$ rails g devise:install

各ファイルに設定を追加

rails g devise:install を実行するとターミナルに下記の文章が出てきます 番号通りに4つの設定を追加していきます
===============================================================================

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 *

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

① config/environments/development.rb の一番下にコードを追加

デフォルトURLの設定
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

② config/routes.rb に下記コードを追加

ルーティングの設定
root to: "home#index"

③ app/views/layouts/application.html.erb に下記コードを追加

フラッシュメッセージの追加
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

④ ターミナルで rails g devise:views を実行

devise 関連の view を作成
$ rails g devise:views

ターミナルで rails g devise userを実行

User model を作成(model名は任意)
$ rails g devise user

rails db:migrateを実行

users テーブルを作成
$ rails db:migrate

以上で devise 関連の設定は終了です!
お疲れさまでした!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?