Question : At Rails, ApplicationController always get BLANK while reading session variable

Dear experts,

With assumption that before_filter below at ApplicationController is correct, I got an issue while trying to read a session variable at Application Controller.
I have verified that  rhtml file could read the value for this session variable (user_id), but ApplicationController is always detecting it as blank.

Below is the ApplicationController and how i verifying it.

class ApplicationController < ActionController::Base
  before_filter :get_user
    def get_user
    if session['user_id']
      @user = User.find(session['user_id'])
      @x = 'have user id'
    else
      @x ='user_id blank'
    end
  end
end

at rhtml file:

                <% if session[:user_id] %>
                SHOULD HAVE
                <% else %>
                DON'T HAVE
                <% end %>
               

                <%= @x%>

and the result is either:

SHOULD HAVE
user_id blank

OR

DON'T HAVE
user_id blank

So in summary either the session variable is blank or not (SHOULD HAVE / DON'T HAVE), ApplicationController is always getting blank.
Could you kindly advice? Thanks & regards.

Answer : At Rails, ApplicationController always get BLANK while reading session variable

go to a script console and type the commands in it see what user will be and @x.
You can also do it this way.
 def get_user
    if session['user_id']
      @user = User.find(session['user_id'])
     breakpoint
      @x = 'have user id'

you then run the script/breakpointer and will get connected to this session while the breakpoint it hit.

Check the instance variables and see what content they have.

Regards
Friedrich
Random Solutions  
 
programming4us programming4us