Rails,Devise:当覆盖RegistrationsController时,current_user为零 [英] Rails, Devise: current_user is nil when overriding RegistrationsController

查看:89
本文介绍了Rails,Devise:当覆盖RegistrationsController时,current_user为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3.2.5,Devise 2.1



使用Devise验证用户,并在创建新用户时遇到问题。



用户属于我在用户模型中使用 before_save 过滤器创建的帐户。这个工作正常,有一段时间。



新代码需要用户的帐户信息作为创建方法的一部分。我依赖于在请求中传递的参数,所以这不是模型逻辑的好候选者。我已经覆盖了Devise :: RegistrationsController#create方法:

  class DeviseCustom :: RegistrationsController< Devise :: RegistrationsController 
def create
super#Call Devise的创建
account = current_user.account#FAIL! (current_user为零)
account.partner_id = current_partner
account.save!
end
end

current_user nil ,导致代码失败。即使在失败的情况下,我可以看到用户和帐户记录正在保存在数据库中 - 日志显示 commit ,并且记录 self.inspect 显示我所有的上下文(参数等等)仍然存在。



我会以为在这个上下文中可以使用 current_user - 刚刚创建的用户的适当方式是什么?



谢谢

解决方案

前言:我从来没有使用过设计。 p>

我的猜测是没有创建current_user对象,因为在调用链中没有原因(没有用户凭据),或者因为它有在#after_save(如果存在的话)中还没有发生。



Devise使用#resource方法来抓取您要保存的当前实例变量(或所以看起来):



https://github.com/plataformatec/devise/blob/master/app/controllers/devise_controller.rb#L16



我会做的是将其更改为:

 类DeviseCustom :: RegistrationsController< Devise :: RegistrationsController 
def create
super#Call Devise的创建
account = resource.account#FAIL! (current_user为零)
account.partner_id = resource.current_partner
account.save!
end
end

你可能想要添加到你的模型:

  attr_accessor:current_partner 

这将允许您从资源(模型)访问current_partner。



希望有帮助!


Rails 3.2.5, Devise 2.1

Using Devise to authenticate Users, and am having a problem when creating a new user.

A user belongs to an Account which I create using a before_save filter in the User model. This works fine and has for a while.

New code requires the user's account information as part of the create method. I rely on a parameter passed in the request, so this is not a good candidate for Model logic. I have overridden the Devise::RegistrationsController#create method:

class DeviseCustom::RegistrationsController < Devise::RegistrationsController
  def create
    super                                  # Call Devise's create
    account = current_user.account         # FAIL! (current_user is nil)
    account.partner_id = current_partner
    account.save!
  end
end

current_user is nil which causes the code to fail. Even in the case of a failure, I can see that the user and account records are being saved in the database -- the logs show the commit, and logging self.inspect shows all my context (params, and much more) is all still present.

I would have thought that current_user would be available in this context -- what's an appropriate way to get at the user I have just created?

Thanks

解决方案

Preface: I've never used devise.

My guess is the current_user object hasn't been created, either because there wasn't a reason to (no user credentials) earlier on in the call chain, or because it hasn't yet happend perhaps in #after_save (if that exists?).

Devise uses a #resource method to grab the current instance variable you're trying to save (or so it looks):

https://github.com/plataformatec/devise/blob/master/app/controllers/devise_controller.rb#L16

What I would do is change the it to:

class DeviseCustom::RegistrationsController < Devise::RegistrationsController
    def create
      super                                  # Call Devise's create
      account = resource.account         # FAIL! (current_user is nil)
      account.partner_id = resource.current_partner
      account.save!
    end
end

You'll probably want to add this to your model:

attr_accessor :current_partner

which will allow you to access the current_partner from the resource (model).

Hopefully that helps!

这篇关于Rails,Devise:当覆盖RegistrationsController时,current_user为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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