简体   繁体   中英

Devise: Pass unconfirmed email address back to sign in page

When a user registers for an account in my Rails app, I'm using the default Devise behavior to send them a confirmation email. On the website, after the user fills out the registration form, they are automatically redirected to the login page with an alert notice that they need to confirm their account via email:

"Please confirm your acount via email."

I would like the alert to be more specific, like

"A confirmation email has been sent to <%= confirmation_email%>. Please click the link in the email to finish the registration process!"

How can I pass the unconfirmed email address back to the view?

I imagine that when you're creating the user, you're still saving the unconfirmed email address in your database when you create/save the user? If so, you should be able to call it the same way you call other variables in the view. Make sure they are defined in the associated controller and then call them up in the view with something like <%= @user.email %>.

Needed to override devise registrations controller create action with this code:

class RegistrationsController < Devise::RegistrationsController 
  # POST /resource
  def create
    build_resource(sign_up_params)
    if resource.save
      # this block will be used when user is saved in database
      if resource.active_for_authentication?
        # this block will be used when user is active or not required to be confirmed
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        # this block will be used when user is required to be confirmed
        user_flash_msg if is_navigational_format? #created a custom method to set flash message
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      # this block is used when validation fails
      clean_up_passwords resource
      respond_with resource
    end
  end

  private

  # set custom flash message for unconfirmed user
  def user_flash_msg
    if resource.inactive_message == :unconfirmed
      #check for inactive_message and pass email variable to devise locals message
      set_flash_message :notice, :"signed_up_but_unconfirmed", email: resource.email
    else
      set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}"
    end
  end
end

Then pass email variable in devise.en.yml file

en:
  devise:
    registrations:
      signed_up_but_unconfirmed: "A confirmation email has been sent to %{email}. Please click the link in the email to finish the registration process!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM