简体   繁体   中英

How can I pass email of unconfirmed user to the session (Devise)?

I have a registration form, and after registration I want browser to remember user's (still unconfirmed) email. How could I do that? I assume I can do this somehow with build_resource of RegistrationsController .

Assuming you want to remember the last registered/un-confirmed user you could do this:

Create new folder under app/controllers called my_devise

Create a file called registrations_controller.rb in app/controllser/my_devise :

class MyDevise::RegistrationsController < Devise::RegistrationsController

  # POST /resource
  def create
    build_resource

    if resource.save
      # here we save the registering user in a session variable
      session[:registered_as] = resource 
      if resource.active_for_authentication?
        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
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

end

Update the config/routes.rb file to tell Devise to use our new controller:

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

After registration, the session variable :registered_as will now hold the last user that registered and could be referenced in any controller or view:

some_view.html.rb:

<p>Registered as: 

<%= session[:registered_as].inspect %>

</p>

See also: Override devise registrations controller

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