Search code examples
ruby-on-railsdevise

Go to a specific page if account is not activated in rails


I have user and account model and the association is user has_one account. For authentication, I'm using devise gem. Whenever a user registered for the first time, an account is also created. But the account will be inactivation state. This will be in active state when the admin gives access to the account. If it is inactivation state then I need the page to redirect to the specific page where it will show Waiting for activation. I have used the below code

def after_sign_in_path_for(resource)
  redirect activation_path
end

But if the user clicks on the other links it should not go to that page, it should be restricted and redirect to the activation path saying that under activation process.


Solution

  • You could, for example, add a before_action for this

    class ApplicationController
      before_action :check_activation
    
      private
    
      def check_activation
        redirect_to activation_path unless current_user.account.active?
      end
    end
    

    TODO:

    • make sure anon users (current_user is nil) are handled properly
    • do not perform the redirect if current page is the activation page (or it would cause a redirect loop)