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

后端 未结 8 2211
傲寒
傲寒 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:36

    The login form is on every pages (top/sidebar) or you have a login page?

    If is on every pages, you can use request.referrer to know where the user came from.

    0 讨论(0)
  • 2020-12-09 06:40

    You can override the after_sign_in_path_for method without losing the desired behavior.

     def after_sign_in_path_for(resource_or_scope)
       stored_location_for(resource_or_scope) || dashboard_path
     end
    

    I would also put a condition in the root_path action that redirects if current_user exists.

    RootController.rb
    
    def index
      if current_user
         redirect_to dashboard_path
      else
        super #or whatever
      end
    end
    

    You could also use a before_filter, if you wanted to add the redirect to many unprotected actions.

    0 讨论(0)
提交回复
热议问题