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

Rails deviseの実装

Posted at

初めに

Railsの場合は、ログイン機能を「devise」というGemを使用することで簡単に実装することができるので、deviseの導入手順を備忘録としてメモ。

作業手順

  1. Gemをインストールしてサーバーを立ち上げ直す
  2. コマンドを利用してdeviseの設定ファイルを作成する
  3. コマンドを利用してUsersモデルを作成する

① Gemfile deviseの指定

Gemfile
gem 'devise'   # 最終行に追記
  • bunle installを実行
ターミナル
$ bundle install
  • ローカルサーバーを再起動
ターミナル
$ rails s

② コマンドを利用してdeviseの設定ファイルを作成する

ターミナル
$ rails g devise:install

config/initializers/deivise.rb
config/locales/divise.en.yml
の2つのファイルが作成される。

③ コマンドを利用してUserモデルを作成

ターミナル
$ rails g devise user
  • config/routes.rbに以下のような記述が自動的に追記される。
routes.rb
Rails.application.routes.draw do
  devise_for :users
# 以下略
  • devise_forについて

devise_forはログインまわりに必要なルーティングを一気に生成してくれるdeviseのヘルパーメソッド。
devise_for :usersの記述によって「ログイン・新規登録」で必要なルーティングが生成される。

  • current_useruser_signed_in?などのヘルパーメソッドが使えるようになる。

④ rake db:migrateを実行する

ターミナル
$ rake 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?