Search code examples
ruby-on-railsruby-on-rails-4devisedevise-confirmable

How to modify Devise's message on a specific step of its workflow?


After Sign Up a new user I get redirected to a Sign In page with the following flash[:alert] message:

"You need to sign in or sign up before continuing."

My User model uses Devise's :confirmable module so it would be nice if after Sign Up a user would see a modified message instead:

"Thanks for signing up. We have sent you a confirmational email. Please check your email"

Is there a way to achieve it?


Notes about Devise workflow:

Currently a user has no idea that a confirmational email was sent to him. He will see Devise's failure message only when he tries to Log In using unconfirmed email address:

"You have to confirm your email address before continuing."


Here's solution: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)

I just followed first 2 steps:

1) Create RegistrationsController:

class RegistrationsController < Devise::RegistrationsController
  protected


  # TODO: will this  method be triggered one day?
  def after_sign_up_path_for(resource)
    # '/an/example/path'
    new_user_session_path
  end

  def after_inactive_sign_up_path_for(resource)
    new_user_session_path
  end
end

2) Change routes to:

devise_for :users, :controllers => {:registrations => 'registrations'}

Solution

  • First, add devise :confirmable to your models/user.rb

    devise :confirmable
    

    Then, do the migration as:

    rails g migration add_confirmable_to_devise
    

    Will generate db/migrate/YYYYMMDDxxx_add_confirmable_to_devise.rb. Add the following to it in order to do the migration.

    class AddConfirmableToDevise < ActiveRecord::Migration
      # Note: You can't use change, as User.update_all will fail in the down migration
      def up
        add_column :users, :confirmation_token, :string
        add_column :users, :confirmed_at, :datetime
        add_column :users, :confirmation_sent_at, :datetime
        # add_column :users, :unconfirmed_email, :string # Only if using reconfirmable
        add_index :users, :confirmation_token, unique: true
        # User.reset_column_information # Need for some types of updates, but not for update_all.
        # To avoid a short time window between running the migration and updating all existing
        # users as confirmed, do the following
        User.all.update_all confirmed_at: Time.now
        # All existing user accounts should be able to log in after this.
        # Remind: Rails using SQLite as default. And SQLite has no such function :NOW.
        # Use :date('now') instead of :NOW when using SQLite.
        # => execute("UPDATE users SET confirmed_at = date('now')")
        # Or => User.all.update_all confirmed_at: Time.now
      end
    
      def down
        remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
        # remove_columns :users, :unconfirmed_email # Only if using reconfirmable
      end
    end
    

    Do the migration rake db:migrate

    If not using reconfirmable, update the configuration in config/initializers/devise.rb

    config.reconfirmable = false
    

    Hope this will help you.