0

In a controller action, I need to check whether the controller is being accessed by the application itself (which would set a session variable current_user), or from an api call. The beginning of my controller looks like this:

   130:   def create
   131:     # handle the possibility that this is an api call
   132:     api_call = current_user.nil?
=> 133:     binding.pry

When I pry in to see what is going on, I get this nonsensical output:

[1] pry(#<Crm::ConnectionsController>)> api_call
=> false
[2] pry(#<Crm::ConnectionsController>)> current_user
=> nil
[3] pry(#<Crm::ConnectionsController>)> current_user.nil?
=> true
[4] pry(#<Crm::ConnectionsController>)> api_call = current_user.nil?
=> true
[5] pry(#<Crm::ConnectionsController>)> api_call
=> true

And when I change the variable api_call from the pry console with the following line, then resume execution, the controller behaves from there on as if api_call is set to true.

Why would this be? Why is my controller not setting api_call correctly in the first place?

1 Answer 1

2

This shouldn't happen at all (I'm one of the Pry developers). The most likely scenario is that current_user is returning a non-nil value the first time you call it, but caching nil to return for all future calls.

If you've double checked that, and you're still having the problem, I'd need to see more of your code to debug it.

Sign up to request clarification or add additional context in comments.

1 Comment

Mad props for having one of the actual authors of pry answer a question on SO, even if it's only to say "I'm not sure." You are the kind of developer we need, even if not the kind we deserve!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.