0

As the title suggests, the devise helper methods are only working in users_controller.rb. If I try to access it in any other controller (even applications_controller.rb) after sign-in, im getting this error.

Routing Error
undefined local variable or method `current_user' for ApplicationController:Class

Im just adding puts current_user in applications_controller.rb.

This is my routes.rb

 devise_for :users
 resources :users

Any idea why this is happening? Thanks.

Chet
  • 134
  • 1
  • 11
  • Possible duplicate of [Rails/devise current\_user accessed in model but it's nil](https://stackoverflow.com/questions/18562143/rails-devise-current-user-accessed-in-model-but-its-nil) – Crashtor Aug 09 '17 at 12:55

1 Answers1

1

current_user is not a class method so it won't be available directly in ApplicationController

it would be available in all the action and before_action

Use the below logic to check the value of the current_user in you Application Controller.

class ApplicationController < ActionController::Base
  before_action do
    puts current_user
  end
  protect_from_forgery with: :exception
end

Please let me know if you still face any issues

Nimish Gupta
  • 3,095
  • 1
  • 12
  • 20