0

I've been messing with my ApplicationController and somehow broke something. My helper ApplicationHelper::current_settings serializes @user so that my JS app can load the current user's data. But suddenly:

undefined method `id' for nil:NilClass
at `@user.id` in app/helpers/application_helper.rb, line 5

However, in my better_errors console, I can see that the variable is set on the controller, just not accessible in the current context:

>> @user
=> nil
>> controller.instance_variable_get("@user")
=> #<Person id: 1, ...>
>> instance_variable_get("@user")
=> nil

I'm setting @organization the same way, and it's there:

>> @organization
=> #<Organization id: 1 ... >

But for some reason, no sign of @user!

Here's the helper:

module ApplicationHelper
  def current_settings
    {
      person: {
        id: @user.id,
        name: @user.to_s
      }
    }.to_json.html_safe
  end
end

And in the controller, it's like:

before_filter :load_user

def load_user
  @user = Person.where(some_query).first
end

Any ideas?? Thanks!

rmosolgo
  • 1,854
  • 1
  • 18
  • 23

1 Answers1

0

Ok, got it. I was loading organization and user in sequence, but my organization load was returning nil, so load_user wasn't called:

before_filter :authorize

def authorize
  load_organization && load_user
end

since load_organization was returning nil, it didn't kill the controller action chain (like returning false would have done) but it also prevented load_user from being called. Derp!

rmosolgo
  • 1,854
  • 1
  • 18
  • 23