ume

rails6 devise confirmationコントローラーのshowアクションに反応させたいのになぜかusersコントローラーのshowアクションに反応する問題

前書き

rails6でdeviseを導入しログイン、ログアウトを簡潔に実装したいと思いました。ただ想定しているコントローラーのアクションが反応しない。なぜか別のコントローラーのアクションを探しに行くという問題に直面しました。同様に悩んでいる方の参考になればと思い記事にします

結論

Routs,rbファイルのルーティングの記載の順番を調べて見せてください

私の場合 users/confirmationというルーティングがリクエストされるとconfirmations/showアクションが反応するように設定していました。

   user_confirmation GET    /users/confirmation(.:format)                                                              devise/confirmations#show

しかし実際にusers/confirmationというurlをリクエストすると なぜかusersコントローラーのshowアクションを探しに行っていました。

その原因はroutes.rbファイルのルーティングの記載の順番でした↓

一部省略                                                                                                                                   users#search
                                   users GET    /users(.:format)                                                                                  users#index
                                         POST   /users(.:format)                                                                                  users#create
                                new_user GET    /users/new(.:format)                                                                              users#new
                               edit_user GET    /users/:id/edit(.:format)                                                                         users#edit
                                    user GET    /users/:id(.:format)                                                                              users#show
                                         PATCH  /users/:id(.:format)                                                                              users#update
                                         PUT    /users/:id(.:format)                                                                              users#update
                                         DELETE /users/:id(.:format)                                                                              users#destroy
                        new_user_session GET    /users/sign_in(.: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
                   new_user_confirmation GET    /users/confirmation/new(.:format)                                                                 devise/confirmations#new
                       user_confirmation GET    /users/confirmation(.:format)                                                                     devise/confirmations#show
                                         POST   /users/confirmation(.:format)   

users/confirmationというリクエストが飛んできたらこの上の表を上から順番に探しに行きます。 users/:idに注目。:idと聞くと数値が入ると思っていました。しかしこれは数値に限ったことではないみたいです。users/:idがusers/confirmationという解釈になり、usersコントローラーのshowアクションを探しに行く結果になりました。

なのでroutes.rbでdeviseで使用しているルーティングをresources :users doの上に記載するとエラーがなくなり想定している結果になりました

Rails.application.routes.draw do
  
  get 'relationships/:id/new',             to:'relationships#new'
  get 'relationships/:id/requests_status', to:'relationships#requests_status',as: :requests_status
  post 'relationships/:id/create',         to:'relationships#create',as: :create_relationships
  get  'relationships/destroy'
  resources :weekly_goals
  resources :tasks
  resources :monthly_goals do 
    member do 
      get 'my_goal'
    end
  end



  #  deviseが用意しているデフォルトのコントローラーを使わず自分の環境のusersディレクトリは配下のコントローラーを使えというメソッド
  devise_for :users,  controllers: {
    registrations:  'users/registrations',
    confirmations:  'users/confirmations',
    sessions:      'users/sessions',
     }
  
     #↓は↑のusersディレクトリは配下のコントローラーにつなげるようにルーティングを作成するメソッド
     devise_scope :user do
      get '/confirmation_email', to: 'users/confirmations#check_email'
      get '/monthly_goals/new',  to: 'users/monthly_goals#new'
      delete '/logout',          to: 'users/sessions#destroy'
      get '/sign_up',            to: 'users/sessions#new'  
      get '/sign_up_account',    to: 'users/registrations#new'  
      post 'register',           to: 'users/registrations#create'  
    end
  
  resources :users do
    collection do
      get 'search' => 'users#search'
    end
  end

   post '/monthly_goals/:id/my_goal', to: 'weekly_goals#create'

 ```