Devise Cheatsheet

Techie     August 2022

1 . Redirecting User After Sign Up (Confirmation Pending)

If you want to redirect the user to a specific url after signing up, override the after_inactive_sign_up_path_for in the registrations_controller.


i). Create a new registrations_controller.rb in app/controllers directory.

# app/controllers/registrations_controller.rb

class RegistrationsController < ::Devise::RegistrationsController
  layout false
  # the rest is inherited, so it should work
  
  private
  def after_inactive_sign_up_path_for(resource_or_scope)
    if resource_or_scope == :user || resource_or_scope.class == User || resource_or_scope == User
      your_pending_registrations_path
    #elsif resource_or_scope == :admin || resource_or_scope.class == AdminUser  || resource_or_scope == AdminUser
    #  admin_root_path
    else
      super
    end
  end
  
end


ii). Override Devise default behaviour in the routes in config/routes.rb

 
# config/routes.rb
 devise_for :users, controllers: { registrations: 'registrations' }
 


2 . Redirecting User From The Confirmation Email

You may want to redirect the user to a specific url after they clicked the link in the confirmation email. To do that, just override the after_confirmation_path_for in the confirmations_controller.


i). Create a new confirmations_controller.rb in app/controllers directory.

# app/controllers/confirmations_controller.rb

class ConfirmationsController < Devise::ConfirmationsController
  private
  def after_confirmation_path_for(resource_name, resource)
    sign_in(resource) # In case you want to sign in the user
    # some_other_new_after_confirmation_path
  end
end


ii). Override Devise default behaviour in the routes in config/routes.rb

 
# config/routes.rb
 devise_for :users, controllers: { confirmations: 'confirmations' }


3 . Update User email without Sending a Confirmation Email If you are using the :reconfirmable attribute and want to update a user’s email without needing a confirmation email to be sent, you can use the skip_reconfirmation! like so:

user = User.where(name: "techietuts")
user.email = 'techie@techietuts.com'
user.skip_reconfirmation!
user.save!


4 . Allowing Unconfirmed Users For A Given Period

You can give unconfirmed users some time to confirm their emails by setting the period for allow_unconfirmed_access_for in the config/initializers/devise.rb file.

# config/initializers/devise.rb

config.allow_unconfirmed_access_for = 30.days


5 . Skip Required Confirmation

You can skip required confirmation even if you’ve already added confirmable for devise.

# in User.rb
protected

def confirmation_required?
  false
end


6 . Allow Users to Login and Confirm their Emails Later

i). Enable reconfirmable and set allow_unconfirmed_access_for in config/initializers/devise.rb file.

# config/initializers/devise.rb

config.reconfirmable = true
config.allow_unconfirmed_access_for = 30.days


ii). Create the column in the users table.

# migrations/[...]devise_create_users
# ...

t.string   :unconfirmed_email # Only if using reconfirmable

# ...


iii). Send users confirmation email like so:

 
 User.find_each { |user| user.send_confirmation_instructions } 
       


Thanks for reading, see you in the next one!