I must be missing something out about the operator && in ruby (which I doubt is the cause of this behavior)?
This code:
anonymous && user_signed_in?
does not generate the output wether it is in the before_filter of the application_controller or in an action of the home controller.
And I do not understant why.
Details:
I added this code in the application controller and it would runs as a before_filter
anonymous = cookies['anonymous']
fresh_session = cookies['session_id'].nil?
rep = (fresh_session || (anonymous && user_signed_in?))
puts "REPONSE : #{rep} session: #{fresh_session} anonym #{anonymous} is there a curr user: #{user_signed_in?} /// BOTH: #{(anonymous && user_signed_in?)}"
the printed resulte is:
REPONSE : true session: false anonym false is there a curr user: true /// BOTH: true
If the same code is in an action of the home controller
the printed result is:
REPONSE : false session: false anonym false is there a curr user: true /// BOTH: false
Please help me understand.
Edit (Following Klochne’s request):
In the app_controller:
Anon-class: String REPONSE : true session: false anonym false is there a curr user: true /// BOTH: true
In the home_controller
Anon-class: FalseClass REPONSE : false session: false anonym false is there a curr user: true /// BOTH: false
So one interprets it as a string for some reason.
Try setting the variables in the before filter as instance variables instead of local variables. That way they will persist and stay set in the controller action as opposed to instance variables. So change the method being called with before_filter to be this:
And then in your controller you can just put this line again:
And it should print out the same results. Local variables are only accessible from within the block of its initialization.