Annoying session problem

Hi, I’ve just run into a very annoying session problem and judging by my failure to find anything on Google it seems that nobody else has experienced this!

I’m trying to set a session variable in application_controller.rb, using simply:

@color = “grey”

session[:colors] = @color

When trying to look at my site I get:

undefined method `session’ for ApplicationController:Class

I’ve used this before and from all the docs this is how I’m supposed to do it! The site was working fine until I tried using session. ApplicationController inherits from ActionController and according to the API documentation, the session stuff is in the actionpack gem which is installed on my system and shows up when I run “bundle install” and “bundle show actionpack”. I’ve also tried “gem update”.

I’m using Rails 3.1.0, Gem 1.8.13, Ruby 1.9.2, running on Ubuntu 11.10

$bundle show actionpack

/usr/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0

I’ve tried this on two separate sites on the same box, one with Apache2 and one with WEBbrick, even running it as root. The second site is a test site with no changes whatsoever made to startup or initialization files. I’ve checked access rights, chmodding everything to 775, restarting the whole system etc etc and I can’t work out why the blessed thing won’t work!

Does anybody have a clue what might be going on?

Regards

Binni

ITAnet

Kirkestien 20 9230 Svenstrup

Telefon: 3020 0868

Email: binni@itanet.nu

WWW: http://www.itanet.nu

image001.gif

You are trying to call ‘session’ as a class method on the ApplicationController class,

which fails, like this:

$ rails c

Loading development environment (Rails 3.1.3)

007:0> ApplicationController.session

NoMethodError: undefined method `session’ for ApplicationController:Class

from (irb):7

from /home/peterv/.rvm/gems/ruby-1.9.3-p0@base_app/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'

from /home/peterv/.rvm/gems/ruby-1.9.3-p0@base_app/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'

from /home/peterv/.rvm/gems/ruby-1.9.3-p0@base_app/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'

from script/rails:6:in `require'

from script/rails:6:in `<main>'

The ‘session’ method is available on instances of the ApplicationController class.

For a single action:

class HomeController < ApplicationController

def index

@color = 'grey'

session[:colors] = @color

end

end

If you wanted to set the color for each and every action, you could do this:

class ApplicationController < ActionController::Base

def set_session_color

@color = 'grey'

session[:colors] = @color

end

before_filter :set_session_color

protect_from_forgery

end

HTH,

Peter

Hi Peter, thanks a lot! It’s really obvious once I’ve had it explained …