6

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.

3
  • What version of Rails are you working with? Also, are these controllers both in the same Rails app, or in different apps? Commented Jul 12, 2009 at 14:27
  • Hi John, I am using rails version is 2.2.2. And my gems version specified in my environment.rb is as follows: ----------- # Specifies gem version of Rails to use when vendor/rails is not present RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION --------------- (I am not using any newer version on my local machine at the moment as these are the current settings on our production server. And I want to test my apps according to them) yes, these controllers and view files belong to the same Rails app. So any idea what's causing this issue? Thanks John Commented Jul 13, 2009 at 3:08
  • Just to make it clear, the session variables that these two controllers are accessing don't have common names. So there is no reason for a session variable to be overwritten unless the secondly loaded web page is storing a new cookie over the existing one(the one that firstly loaded page has stored) ? This is the only logical reason I can think of. But how can you fix something like this? anyone encountered a similar situation before? Commented Jul 13, 2009 at 3:20

1 Answer 1

5

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.

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

8 Comments

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?
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.
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.
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.
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.
|

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.