Rails Devise - Current_User Is Nil

Rails Devise - current_user is nil

It turned out the AJAX request I was making didn't carry the CSRF token. For that reason, Rails was killing my session.

I added skip_before_filter :verify_authenticity_token in my SubscriptionsController and it is now working. It might not be the most secure solution, but it works for now, so I continue to develop and come back to this issue later.

Rails, Devise: current_user is nil when overriding RegistrationsController

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 - Current_user is nil when delete a Pain

Ok i found the solution, in my ApplicationController i call the helper_method :current_user

class ApplicationController < ActionController::Base
helper_method :current_user

def current_user
@current_user ||= User.find_by(id: session[:user])
end
end

Now i can access to current_user in my Prayer model.

Thanks for all guys

with rspec & devise , Why current_user is nil?

In your spec you are using the build method to reference your user. This does not persist the user in the database and so Devise cannot find a user in the database and thus current_user is nil. You should use create instead.

Also you should ensure that you have config.include Warden::Test::Helpers in your rails_helper.rb and use login_as(user) to actually log your user in. See here for more info about feature specs with Devise.

User signed in, but set to nil using Rails and Devise

Solution:
my current_user method in the application controller was overriding devise's own current_user method. I removed the current_user method I had in my application controller and it now shows:

current_user :#<User id: 2, email: "test@tester.com", username: "", admin: false, created_at: "2018-01-07 20:09:17", updated_at: "2018-01-07 21:33:51">
session id request :all
user_signed_in? :true
current user present? :true

found answer here: devise - can't display sign in or sign out in rails view



Related Topics



Leave a reply



Submit