views:

3922

answers:

1

Hi, I am having a problem with using session variables. I have two controllers named 'graduate_students_controller' and 'current_students_controller'. Each of these controllers control different view files. I am using session variables with both these controllers to store session information.

Here's the problem. Let's say I have two view files 'reports/current_students_list', 'reports/graduate_students_list' each controlled separately by the above mentioned controllers.

Now if I try to open those two web pages from within the same browser and try to work with them simultaneously, I get 'nil object access' error from the firstly loaded page. The 'nil object' refers to a session variable that the first page is supposed to access. However, when I use any of those two web applications individually, they work fine.

So its seems to me that session variables of the firstly loaded web app. are getting overwritten by the secondly loaded web app. maybe because the second page stores a new cookie over the first one?

How do I fix this?

Any suggestion is much appreciated.

To clarify a bit more: The two controllers belong to the same Rails application. And I am not using identical session variable names within both controllers. So I cannot see why they can get overwritten

I am new to rails and I would really appreciate some help with this problem. Thanks.

+1  A: 

I'm not sure if you are running two apps, or are referring to two controllers under the same app. If you are looking at different web apps, then I think you are using the same name and session key in your environment for each of these apps. Try changing the key value in your environment.rb:

config.action_controller.session = { :key => "_myapp_session", :secret => "..." }

If you are using the same session variable from two different controllers in the same application, then you'll need to write your code to accomodate this, though I wouldn't recommend doing this. When accessing your session data, check for nil values:

session[:some_key].nil?

and make sure that common code (i.e. in the application_controller.rb) isn't overwriting your values.

Mr. Matt
Hi Matt, I am referring to two controllers in the same application. When you say "same session variables" are you referring to identical session variable names used by both controllers? No I am not doing that, the two controllers are using different names for their session variables. I had a look at my application.rb and sure enough I found this: session :session_key => '_StudDab_session_id'So I guess this is the problem? Each time I load a new page the current session gets overwritten? Can I fix this by specifying unique session keys inside each of the two controllers?
Vicer
No - the session key needs to be unique to each app, not the controllers in the same application. I think there is a bug somewhere in your app that is either clearing out the value in your session. I'd recommend logging the value of the session variables at the top of your controller action, then logging them again at the end. See if they change, and trace it back from there.
Mr. Matt
Hi Matt changing the session key did fix the problem. I will try to investigate and find out what was causing it. It is quite a frustrating prob. Thanks for your suggestions Matt.
Vicer
When I inserted "p session :session_key" at the end of each controller I get:.. [{:session_key => "_my_session_id"}, {:disabled => true}, {:disabled => true}]..does this mean anything? I am new to ruby. When I put "p session :session_key" at the beginning, I get the same output but with just one "{:disabled => true}" instead of two.
Vicer
Oh sorry my mistake. When I said changing the session key i meant setting a session key in each controller. I thought it fixed the prob. but it hasn't :(
Vicer
Sorry - I didn't mean the :session_key, but rather the values stored in the session: session[:some_key_here]Log those at the beginning and end of your controller action, and see when they change.
Mr. Matt
Hi, yes I found that reset_session is being called in each controller when each page is first loaded.You were right. I changed that so the controller will reset only the variables it has declared and not the entire collection when it gets called. Thanks a lot for helping me see the problem.
Vicer
Ahhh - that warm fuzzy feeling of a problem getting fixed. Love it.
Mr. Matt