LoginSignup
0
0

More than 3 years have passed since last update.

deviseのインストールからカラム追加まで【ログイン機能作成】

Posted at

deviseのインストール

terminal
rails g devise install

これだけでdeviseをインストールすることができます。

deviseインストールをしたらすること

terminal
rails g devise install

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

Some setup you must do manually if you haven't yet:

  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.

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

       root to: "home#index"

  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>

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

       rails g devise:views

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

rails g devise:installをすると、上記のようにメッセージが出てきます。
これを1つずつ解説していきます。結論からいうと、全部やる必要はありません。開発に合わせて取捨選択してください。
私が必要だと思うものには☆が付いています。

1.☆認証メールのURLを設定する

config/environments/development.rb
Rails.application.cofigure do 
   #省略
   #一番下に追加
   config.action_mailer.default_url_options = { protocol: 'https', host: 'トップページアドレス' }
end

このトップページアドレスとは、rootのurlのことです。
私の開発環境はcloud9ですが、rails sをして、previewをし、ブラウザで表示をしたら出てくるurlのことです。
例;https://xxxxxxxxxxxx.vfs.cloud9.ap-southeast-1.amazonaws.com

2.rootのurlを決める

deviseをインストールする前にrootをどのアクションにするかは決めている人もいるでしょう。そういう方は読み飛ばして大丈夫です。
rootのurlを決めるには、config/routes.rbにroot to:"top#index"のように記載します。

config/environments/development.rb
Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root to: "top#index"
  #ここのto:はなくてもいい
end

3.フラッシュメッセージをビューに埋め込む。

フラッシュメッセージとは、ログイン時などに出てくる、「ログインに成功しました」や「ログアウトしました」などのユーザーへの簡易的な通知です。
以下は例です。フラッシュメッセージを作りたい人だけみてください。
deviseでのフラッシュメッセージ利用
こちらの記事に詳しく書いてあります。
簡潔に説明すると、
1. views/layouts/にフラッシュメッセージ用のビューファイルを作成。
2. views/layouts/application.html.erbでrenderで読み込む。
3. cssを適用
4. デフォルトでは英語なので、日本語にする。

4.☆deviseのビューファイルを読み込む

terminal
rails g devise:views

これでdeviseのビューファイルを読み込みます。
そうすると、デフォルトのビューファイルを作成してくれます。
さらに、
ruby:terminal
rails g devise user

をしてuserモデルを作成します。
この時にroutes.rbが更新されます。

routes.rb
Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root to: "top#index"
end

devise_for :usersというdeviseのヘルパーメソッドが記載されているため、新しく作成されたビューファイルに対して、ルーティングを設定する必要はありません。

rake db:migrateをおこないます。

カラムの追加

terminal
rails g migrate AddDetailsToUser name:string birthday:date

今回はnameとbirthdayカラムを追加しました。

カラムの追加に関しては、こちらの記事に詳しく書いてあります。
カラムの追加と削除

読んでくださってありがとうございました!

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