Rails, Devise: current_user is nil when overriding RegistrationsController -
rails 3.2.5, devise 2.1
using devise authenticate users, , having problem when creating new user.
a user belongs account create using before_save
filter in user model. works fine , has while.
new code requires user's account information part of create method. rely on parameter passed in request, not candidate model logic. have overridden devise::registrationscontroller#create method:
class devisecustom::registrationscontroller < devise::registrationscontroller def create super # call devise's create account = current_user.account # fail! (current_user nil) account.partner_id = current_partner account.save! end end
current_user
nil
causes code fail. in case of failure, can see user , account records are being saved in database -- logs show commit
, , logging self.inspect
shows context (params, , more) still present.
i have thought current_user
available in context -- what's appropriate way @ user have created?
thanks
preface: i've never used devise.
my guess current_user object hasn't been created, either because there wasn't reason (no user credentials) earlier on in call chain, or because hasn't yet happend perhaps in #after_save (if exists?).
devise uses #resource method grab current instance variable you're trying save (or looks):
https://github.com/plataformatec/devise/blob/master/app/controllers/devise_controller.rb#l16
what change to:
class devisecustom::registrationscontroller < devise::registrationscontroller def create super # call devise's create account = resource.account # fail! (current_user nil) account.partner_id = resource.current_partner account.save! end end
you'll want add model:
attr_accessor :current_partner
which allow access current_partner resource (model).
hopefully helps!
Comments
Post a Comment