设计:将现有的访客用户转换为注册用户 [英] Devise: Convert an existing guest user to a registered user

查看:116
本文介绍了设计:将现有的访客用户转换为注册用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



https://github.com/plataformatec/devise/wiki/How-To:-Create-a-est -user



这允许每个用户在我的网站上使用一些基本的东西,例如创建评论。



现在,当用户决定注册时,我想将访客用户转换为注册用户。



我试图实现我自己的 User.new_with_session 方法,如下所示:

  def self.new_with_session(params,session)
user = User.find(session [:guest_user_id])

user.name = params [:name]
user.email = params [:email]
user.password = params [:password]
user.password_confirmation = params [:password_confirmation]
user.created_at = nil
用户。 updated_at = nil
user.confirmed_at = nil
user.guest = false

用户
营救ActiveRecord :: RecordNotFound
超级
结束

但这不行。不知何故,在提交表单时,设法似乎检测到用户已经存在于DB中,因此甚至不会尝试对其进行验证等,但只是退出注册过程(因为它正在考虑用户已经登录),并转发到其他页面,导致您需要登录或注册才能继续。



据我所知,这发生在之前 Devise :: RegistrationsController#create 甚至被调用。



所以这是我的问题:有没有办法告诉Devise不要认为用户登录因为用户已经存在于数据库中(尽管在提交注册表后,我不知道Devise如何知道用户)?



更新



我可以进一步跟踪问题。每当 self.new_with_session 返回持久记录时,注册过程将被取消,如上所述。返回新对象时,注册继续。但是我无法确定这是在哪里检查...!

解决方案

我重写了RegistrationsController create方法像这样:

  class RegistrationsController< Devise :: RegistrationsController 
def create
如果user = User.find_by_email params [:user] [:email]
user.update_attributes params [:user]
sign_in_and_redirect用户,旁路: true
else
super
end
end
end

我只是找到用户并更新它们。 Devise还有一个助手,将 sign_in 方法与重定向相结合。



如果没有找到用户,则继续正常。


I'm logging every new user automatically into the site by creating a guest user as described here:

https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user

This allows every user to use some basic stuff on my site, e.g. creating comments.

Now, when the user decides to register, I want to convert the guest user into a registered user.

I tried to implement my own User.new_with_session method like this:

def self.new_with_session(params, session)
  user = User.find(session[:guest_user_id])

  user.name                  = params[:name]
  user.email                 = params[:email]
  user.password              = params[:password]
  user.password_confirmation = params[:password_confirmation]
  user.created_at            = nil
  user.updated_at            = nil
  user.confirmed_at          = nil
  user.guest                 = false

  user
rescue ActiveRecord::RecordNotFound
  super
end

But this doesn't work. Somehow, when submitting the form, devise somehow seems to detect that the user already exists in the DB, and so doesn't even try to do validations on it, etc., but simply exits the sign up process (because it's thinking the user is already signed in) and forwards to some other page, which results in a "You need to sign in or sign up before continuing." flash message.

As far as I can tell, this happens somewhere before Devise::RegistrationsController#create is even called.

So here's my question: is there a way to tell Devise not to think a user is logged in simply because the user already exists in the DB (although I don't have an idea how Devise knows about the user after submitting the registration form)?

Update

I could track the problem down a bit further. Whenever self.new_with_session returns a persisted record, the registration process is cancelled, as described above. When returning a new object, the registration continues. But I can't figure out where this is checked upon...!?

解决方案

I'm overriding the RegistrationsController create method like so:

class RegistrationsController < Devise::RegistrationsController
  def create
    if user = User.find_by_email params[:user][:email]
      user.update_attributes params[:user]
      sign_in_and_redirect user, bypass: true
    else
      super
    end
  end
end

I'm just finding the user and updating them. Devise also has a helper that combines the sign_in method with a redirect.

If no user is found then carry on as normal.

这篇关于设计:将现有的访客用户转换为注册用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆