1
使用rails 4.1.3和devise 3.0.4体验一个非常奇怪的错误。试图处理匿名登录。无论是用户通过设计登录还是以前一种形式填写了姓名和电子邮件。current_user在rails/devise中设置为零
继在控制器代码:
logger.debug "current_user before:#{current_user.inspect}"
if(current_user.nil?)
current_user = User.create(:last_name => params[:guest_username], :email => params[:guest_email])
logger.debug "Guest user created"
end
logger.debug "current_user after:#{current_user.inspect}"
下面的日志输出:
D, [2014-10-30T14:10:15.627130 #2148] DEBUG -- : current_user before:#<User _id: 52ffe1b35043001fa8000000, created_at: 2014-02-15 21:52:51 UTC, updated_at: 2014-10-30 13:00:47 UTC, email: "[email protected]", encrypted_password: "$2a$10$r5wXs97N.HIAp9bp5GZBaOUt8R5.S/Z/2PhcpF0xaIVuTD4BpZrmO", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 134, current_sign_in_at: 2014-10-30 13:00:47 UTC, last_sign_in_at: 2014-10-30 12:57:35 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: 2014-02-15 21:53:04 UTC, confirmation_sent_at: 2014-02-15 21:52:51 UTC, unconfirmed_email: nil, image: "", first_name: "Dave", last_name: "Something", roles_mask: 7>
D, [2014-10-30T14:10:15.629130 #2148] DEBUG -- : current_user after:nil
原来这就是我不明白,为什么是CURRENT_USER对象被重置到零,即使代码中的if块没有被执行...
发生了什么事?
预先感谢迈克 -
嗨,我的问题是用户正确登录时t通过设计,因此current_user被定义。不知何故它被销毁,请参阅日志输出。我不明白为什么...... – Mike 2014-10-30 13:47:17
这是因为你试图自己创建用户,让devise的会话控制器为你处理它,如果你必须改变它的代码,那么确保你有完全相同或几乎相同的实现的代码。 – Surya 2014-10-30 13:49:37