1

我有一个注册表单,注册后我希望浏览器记住用户(仍未确认)的电子邮件。我怎么能那样做?build_resource我想我可以用of以某种方式做到这一点RegistrationsController

4

1 回答 1

0

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

在 app/controllers 下创建名为my_devise的新文件夹

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>

另请参阅:覆盖设计注册控制器

于 2013-04-08T11:31:33.267 回答