Search code examples
authenticationdevise

redirect after logging in rails to desired page


below my the app controller and routes, normally when i open localhost:3000 its show me the positions/new page, now i get redirected to the login page which is good, but after logging in it does nothing .... while it should be routed to 'positions#new'

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end

routes:
Rails.application.routes.draw do
  devise_for :users

  root to: 'positions#new'

resources :positions

end


Solution

  • If u are using Devise you need to overwrite after_sign_in_path method in your SessionsController < Devise::SessionsController

    def after_sign_in_path "/positions/new" end

    Your SessionController should look like this:

    class SessionsController < Devise::SessionsController
    
      private
    
      def after_sign_in_path_for(resource)
        "/positions/new"
      end
    end