Redirect user after log in only if it's on root_path

后端 未结 8 2318
傲寒
傲寒 2020-12-09 06:13

I have a root_path on my Rails application that is not user-protected i.e. it\'s a simple portal homepage, with a login form.

After the users log in, I\

8条回答
  •  星月不相逢
    2020-12-09 06:28

    It sounds like you're over complicating the issue. If you get into overriding routing variables it just leads to headaches down the line. I would recommend using a before filter to require a login and use the except param or skip that before filter for your landing page if you're using a separate controller. As an example:

    class ApplicationController < ActionController::Base
    
      before_filter :require_login, :except => :root
    
      def root
        # Homepage
      end
    
      protected
    
      def require_login
        redirect_to login_path and return unless logged_in?
      end
    
    end
    

    (Make sure you have logged_in? defined)

    If you are using a separate controller it will look something like this:

    class HomepageController < ApplicationController
    
      skip_before_filter :require_login
      before_filter :route
    
      protected
    
      def route
        redirect_to dashboard_path and return if logged_in?
      end
    
    end
    

    Regarding proper routing after a login, that would come down to what you're doing when you're creating your session. Regardless, this setup should catch anyone that's logged in trying to hit the homepage, and route them to your dashboard and anyone trying to hit restricted content (Anything besides root) and route them to the login_path

提交回复
热议问题