# How do I access the session in a rails plugin?

**URL:** <https://www.ruby-forum.com/t/how-do-i-access-the-session-in-a-rails-plugin/97158>\
**Category:** Rails\
**Created:** [July 5, 2006, 10:47am UTC](https://www.ruby-forum.com/t/how-do-i-access-the-session-in-a-rails-plugin/97158 "2006-07-05T10:47:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_MacCaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@Alex\_MacCaw](https://www.ruby-forum.com/u/Alex_MacCaw)\
**Post date:** [July 5, 2006, 10:47am UTC](https://www.ruby-forum.com/t/how-do-i-access-the-session-in-a-rails-plugin/97158/1 "2006-07-05T10:47:45Z")

</div>

I’m trying to access the session in a rails plugin, so as to set it as  
below.

1. module Juggernaut
2. 
3. def self.set\_channels(chan)
4. session[:juggernaut\_channels] = chan
5. end
6. end
7. 
8. module ActionController
9. class Base
10. include Juggernaut
11. end
12. end

At the moment I get: undefined local variable or method `session’ for  
Juggernaut:Module

---

<div class="post-metadata">

**Author:** ![Alex\_MacCaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@Alex\_MacCaw](https://www.ruby-forum.com/u/Alex_MacCaw)\
**Post date:** [July 5, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/how-do-i-access-the-session-in-a-rails-plugin/97158/2 "2006-07-05T13:54:50Z")

</div>

Here’s an example of session access within a module included in a  
controller (which could be witihin a plugin or not).

module LogSession  
def log\_session  
logger.info(session.inspect)  
end  
end

class ApplicationController \< ActionController::Base  
include LogSession  
before\_filter :log\_session  
end

Tom

---

<div class="post-metadata">

**Author:** ![Alex\_MacCaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@Alex\_MacCaw](https://www.ruby-forum.com/u/Alex_MacCaw)\
**Post date:** [July 5, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/how-do-i-access-the-session-in-a-rails-plugin/97158/3 "2006-07-05T17:07:28Z")

</div>

With the code you’ve got here, set\_channels is a method of the module  
Juggernaut (i.e. invokable through Juggernaut.set\_channels), and  
therefore won’t be added to any classes which include the Juggernaut  
module as instance methods. Is that what you wanted?

Basically, with your current code, the value of ‘self’ at the point of  
execution for ‘Juggernaut.set\_channels’ is the module Juggernaut,  
which doesn’t have any variable or method ‘session’. The ‘session’  
variable or method I’m guessing that you’re after is the one available  
in ActionController::Base, and all subclasses. David Black’s book  
explains this very well, and I can highly recommend it if you want to  
understand how classes, modules, objects and methods interact.

Typically in a plugin you might do something like this (this could all  
be in init.rb, not that such is good style but it should work):

module Juggernaut  
def set\_channels(chan) # note - no ‘self’  
sessions[:juggernaut\_channels] = chan  
end  
end

ActionController::Base.send(:include, Juggernaut)

At this point, once the plugin is loaded, all of your controllers have  
a ‘set\_channels’ _instance_ method available, so you can do:

class MyController \< ApplicationController  
def some\_action  
# … stuff…  
set\_channels(some\_arg)  
# … other stuff…  
end  
end

HTH

- james
