Search code examples
ruby-on-railsrubydevise

Rails unable to override Devise Passwords Controller


I am trying to override the passwords controller in Devise and when changing the devise_for :users routes in my routes.rb file the routes are not being populated when running rails routes.

I have a file called users/passwords_controller.rb with a puts in there as an example.

class User::PasswordsController < Devise::PasswordsController
    def create
        puts 'creating user profile'
    super
    end
end

Here is the route defination.

devise_for :users, controllers: {
    sessions:           'user/sessions',
    passwords:          'user/passwords',
    registrations:      'user/registrations'
  }

The rails routes command returns these and only these devise routes.

new_user_session GET    /users/sign_in(.:format)                                                                          user/sessions#new
                            user_session POST   /users/sign_in(.:format)                                                                          user/sessions#create
                    destroy_user_session DELETE /users/sign_out(.:format)                                                                         user/sessions#destroy
                cancel_user_registration GET    /users/cancel(.:format)                                                                           user/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                                                                          user/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                                                                             user/registrations#edit
                       user_registration PATCH  /users(.:format)                                                                                  user/registrations#update
                                         PUT    /users(.:format)                                                                                  user/registrations#update
                                         DELETE /users(.:format)                                                                                  user/registrations#destroy
                                         POST   /users(.:format)                                                                                  user/registrations#create

When running rails routes I would expect to see the passwords routes, however they are not there and this leads to 404's when trying to hit these resources. I am unable to find any documentation about this behavior anywhere. I am currently running Ruby version 3.1.2, Rails version 7.0.4.2, and Devise version 4.8.1.

I have looked into this post, however it does not describe my problem and is over 10 years old at this point.


Solution

  • Make sure you have :recoverable in you User model:

    class User < ApplicationRecord
      devise :database_authenticatable, :recoverable # there's also :trackable, :validatable, :confirmable
    end
    

    This enables the passwords routes.