如何将未确认用户的电子邮件传递给会话(设计)?

时间:2013-04-08 10:17:24

标签: ruby-on-rails email session devise devise-confirmable

我有一份注册表,注册后我希望浏览器记住用户(仍然未经证实)的电子邮件。我怎么能这样做?我假设我可以使用build_resource的{​​{1}}以某种方式执行此操作。

1 个答案:

答案 0 :(得分:0)

假设您想要记住上次注册/未确认的用户,您可以这样做:

在名为 my_devise

的app / controllers下创建新文件夹

app / controllser / my_devise 中创建名为 registrations_controller.rb 的文件:

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

更新 config / routes.rb 文件,告诉Devise使用我们的新控制器:

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

注册后,会话变量:registered_as 现在将保留最后注册的用户,并且可以在任何控制器或视图中引用:

some_view.html.rb:

<p>Registered as: 

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

</p>

另请参阅:Override devise registrations controller