Ruby on Rails Wednesday, December 28, 2011

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

 

Fra: rubyonrails-talk@googlegroups.com [mailto:rubyonrails-talk@googlegroups.com] På vegne af Peter Vandenabeele
Sendt: 28. december 2011 14:28
Til: rubyonrails-talk@googlegroups.com
Emne: Re: [Rails] Annoying session problem

 

On Wed, Dec 28, 2011 at 1:44 PM, Brynjolfur Thorvardsson <binni@itanet.nu> wrote:

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?

 

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

 

--
Peter Vandenabeele
http://twitter.com/peter_v

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

 

No comments:

Post a Comment