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.

サインアップ サインイン機能の実装

Posted at

目標物はこちら

Image from Gyazo
↑サインアップ画面

Image from Gyazo

↑サインイン画面

目標物を作成するまでの流れ
1、DeviseのGemをインストール
2、Deviseのビューファイルをインストール
3、Userモデルを作成
4、Usersテーブルを作成
*フラッシュメッセージ追加など必要に応じて行います

1、DeviseのGemをインストール

まず今回はDeviseを導入しますので deviseの公式ドキュメントにアクセスします
公式ドキュメント) https://github.com/plataformatec/devise
アクセスしたら「Getting started」の箇所を見る

Image from Gyazo

「Add the following line to your Gemfile」とあるので、まずGemfileの最下部に以下のコードを追加します。

Gemfile
gem 'devise'
ターミナル
$ bundle install

次に「Next, you need to run the generator」と公式ドキュメントにあるように以下のコマンドを実行

ターミナル
$ rails generate devise:install

deviseを使う際はdevise用のコマンドを打つ必要があり、それが↑のコマンドです。
*deviseで使用するモデルを作成するときは、rails g modelのような通常のモデル作成とはコマンドが異なります。
ターミナルに以下の表示が出てくるはずです。
こちらがdeviseを使う際の初期設定です
Image from Gyazo
1番  →パス
2番 ルーティングの設定 →確認
3番 フラッシュメッセージを実装する場合 指示通り app/views/layouts/application.html.hamlに修正を加えます。

2、Deviseのビューファイルをインストール

4番 Deviseのビューファイルのダウンロードです。ターミナルで表示されている通り以下のコマンドを実行します。

$ rails g devise:views

Image from Gyazo
このようにビューファイルが生成されます。

Userモデルを作成

devise用のモデルを作成します。名前は任意ですが今回はUserで作成します。

ターミナル
$ rails g devise User

Image from Gyazo

4、Usersテーブルの作成

Userモデルを作成した際にdb/migrateフォルダ内にマイグレーションファイルが生成されています。
とりあえず初期設定のまま rails db:migrate を実施します

$ rails db:migrate

すると
Image from Gyazo

という画面になります
サーバーをcontroll+cで切って rails s で再起動
http://localhost:3000/users/sign_up にアクセスして表示を確認します。
サインインの場合は
http://localhost:3000/users/sign_in で表示を確認します。

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?