Search code examples
ruby-on-railsrubydevise

Unable to redirect from one page to other using devise


Hope everyone doing well.

Actually I am a newbie to Rails. I am making a MCQS application for practice. I used Devise for authentication and I created a user model and controller and linked them with Devise.

Without using the Devise controller my application is working well, but I am unable to redirect from one page to my intended page.

Like: I want to redirect from signup to login page but it's not working.

When I use the Devise controller it says argument error and when I remove argument error it shows params error.

I don't know what to do, please help me how can I redirect from one page to other?

Thanks.


Solution

  • You need to change your routes for devise registrations controller

    devise_for :users, controllers: { registrations: 'registrations' }
    

    And add this RegistrationsController to your controllers and after_sign_up_path_for method to it with path to redirect.

    # app/controllers/registrations_controller.rb
    
    class RegistrationsController < Devise::RegistrationsController
      protected
    
      def after_sign_up_path_for(resource)
        new_user_session_path
      end
    end
    

    Note: If you use devise :confirmable in your User model you need to overwrite after_inactive_sign_up_path_for

    # app/controllers/registrations_controller.rb
    
    class RegistrationsController < Devise::RegistrationsController
      protected
    
      def after_inactive_sign_up_path_for(resource)
        new_user_session_path
      end
    end
    

    And don't forget to restart your server.