Overriding Devise Passwords Controller

15,260

Solution 1

The password controller is extended from devise password controller. So, extend the password controller with devise password controler.

class PasswordsController < Devise::PasswordsController
  ......................

end

Change the routes for password controller with devise

devise_for :users, :controllers => { :passwords => "passwords" }

and the routes will be like this :-

user_password       POST   /password(.:format)       Passwords#create

new_user_password   GET    /password/new(.:format)   Passwords#new

edit_user_password  GET    /password/edit(.:format)  Passwords#edit
                    PUT    /password(.:format)       Passwords#update

Solution 2

If you would like to keep a namespace, try:

# routes.rb
devise_for :users, controllers: { passwords: 'users/passwords' }

# users/passwords_controller.rb
class Users::PasswordsController < Devise::PasswordsController
  ...
end
Share:
15,260
Nick Ginanto
Author by

Nick Ginanto

Updated on June 04, 2022

Comments

  • Nick Ginanto
    Nick Ginanto about 2 years

    I want to disable the

      def create
        self.resource = resource_class.send_reset_password_instructions(resource_params)
    
        if successfully_sent?(resource)
          respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
        else
          respond_with(resource)
        end
      end
    

    so it won't redirect at all after sending the reset password

    So, I created a new file under app/controllers/users/ called passwords_controller.rb

    which looks like this

    class User::PasswordsController < Devise::PasswordsController
    
      def create
        self.resource = resource_class.send_reset_password_instructions(resource_params)
    
        if successfully_sent?(resource)
          flash[:notice] = "sent password"
        else
          respond_with(resource)
        end
      end
    
      def new
        super
      end
    
      def update
        super       
      end
    
      def edit
        super   
      end
    end
    

    and changed in my routes to

     devise_for :users, :controllers => { :invitations => 'users/invitations', :passwords => 'users/passwords' }
    

    I also have the devise_invite gem..

    When I click on a link for forgotten password I get this error

    Started GET "/users/password/new" for 127.0.0.1 at 2012-11-16 10:21:07 +0200
    
    ActionController::RoutingError (uninitialized constant Users::PasswordsController):
    

    my rake routes are

                  user_password POST   /users/password(.:format)                  users/passwords#create
              new_user_password GET    /users/password/new(.:format)              users/passwords#new
             edit_user_password GET    /users/password/edit(.:format)             users/passwords#edit
                                PUT    /users/password(.:format)                  users/passwords#update
    

    the link in the view is

    <%= link_to "Forgot your password?", new_password_path(User) , :class => "control-group", :style => "position: absolute; bottom: 0", :id=>"forgotpass" %>
    

    What am I missing?