【備忘録】deviseを使ったrailsログイン機能のまとめ①

deviseを使ったログイン機能について自分の理解した部分をまとめていきます。

以下のサイトを参考にしました。
参考サイト:[【Ruby on Rails】gemのdeviseを使用し、名前とパスワードのみでログインする方法](https://qiita.com/japwork/items/275110b810965fbdf72f)

#こんな人の役に立ったら嬉しい

+ ProgateのRuby on Railsを終了したての人
+ これからRailsでポートフォリオの作成を行う人

#ルーティング（route.rb）について
以下のroutes.rbに対するルーティングを

``` app/routes.rb
Rails.application.routes.draw do
  devise_for :users, controllers: {
    sessions: 'users/sessions',
    registrations: 'users/registrations',
  }
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  root 'homes#top'
  get 'mypage', to: 'homes#about'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

```

以下のコマンドでどのようなルーティングとなっているか確認しましょう。

```
$ rails routes
```

実行結果↓

```
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                     new_user_session GET    /users/sign_in(.:format)                                                                 users/sessions#new
                         user_session POST   /users/sign_in(.:format)                                                                 users/sessions#create
                 destroy_user_session DELETE /users/sign_out(.:format)                                                                users/sessions#destroy
                    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                      PUT    /users/password(.:format)                                                                devise/passwords#update
                                      POST   /users/password(.:format)                                                                devise/passwords#create
             cancel_user_registration GET    /users/cancel(.:format)                                                                  users/registrations#cancel
                new_user_registration GET    /users/sign_up(.:format)                                                                 users/registrations#new
               edit_user_registration GET    /users/edit(.:format)                                                                    users/registrations#edit
                    user_registration PATCH  /users(.:format)                                                                         users/registrations#update
                                      PUT    /users(.:format)                                                                         users/registrations#update
                                      DELETE /users(.:format)                                                                         users/registrations#destroy
                                      POST   /users(.:format)                                                                         users/registrations#create
                                 root GET    /                                                                                        homes#top
                               mypage GET    /mypage(.:format)                                                                        homes#about
        rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                  action_mailbox/ingresses/postmark/inbound_emails#create
           rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                     action_mailbox/ingresses/relay/inbound_emails#create
        rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                  action_mailbox/ingresses/sendgrid/inbound_emails#create
  rails_mandrill_inbound_health_check GET    /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#health_check
        rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#create
         rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                              action_mailbox/ingresses/mailgun/inbound_emails#create
       rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#index
                                      POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#create
    new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                             rails/conductor/action_mailbox/inbound_emails#new
   edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                        rails/conductor/action_mailbox/inbound_emails#edit
        rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#show
                                      PATCH  /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      PUT    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                      rails/conductor/action_mailbox/reroutes#create
                   rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
            rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
                   rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
            update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
                 rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create
```

#viewについて
まず、users下にできるviewファイルには以下があります。

+ homes
`top.html.erb`
`about.html.erb`
自分はファイル名を mypage -> about に変更してます。

+ users
`confirmations/new.html.erb`
`mailer/confirmation_instructions.html.erb`
`mailer/email_changed.html.erb`
`mailer/password_change.html.erb`
`mailer/reset_password_instructions.html.erb`
`mailer/unlock_instructions.html.erb`
`passwords/edit.html.erb`
`passwords/new.html.erb`
`registrations/edit.html.erb`
`registrations/new.html.erb`
`sessions/new.html.erb`
`shared/_error_message.html.erb`
`shared/_links.html.erb`
`unlocks/new.html.erb`


#controllerについて

`homes_controller.rb` 

+ users/  
`confirmations_controller.rb`
`omniauth_callbacks_controller.rb`
`passwords_controller.rb`   
`registrations_controller.rb`   
`sessions_controller.rb`    
`unlocks_controller.rb`    



# 新規登録の動作

###1.1. 
`localhost:3000`に接続

###1.2. 
`routes.rb`の

```
root GET    /  homes#top
```

を参照して `home_controller.rb`の`top` -> `views/homes/top.html.erb`を表示する。

###1.3. 
新規登録をクリック

###1.4. 
`top.html.erb`の`<%= link_to '新規登録', new_user_registration_path %>`によって、`routes.rb`の以下の行を参照して

```
new_user_registration GET    /users/sign_up(.:format)  users/registrations#new
```

`users/regstrations_controller.rb`にnewがないので、何も実行せずに`users/registrations/new.html.erb`を表示。

###1.5. 
`name`,`email`,`password`,`確認用password`を入力して新規登録をクリック。

###1.6.
```
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
```
この部分は調べてもわからなかった。また今度詳しく調べる。

###1.7.
DBに入力した情報が登録されて`mypage.html.erb`を表示する。