# Cookies in custom class

**URL:** <https://www.ruby-forum.com/t/cookies-in-custom-class/147707>\
**Category:** Rails\
**Created:** [September 4, 2008, 1:50pm UTC](https://www.ruby-forum.com/t/cookies-in-custom-class/147707 "2008-09-04T13:50:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [September 4, 2008, 1:50pm UTC](https://www.ruby-forum.com/t/cookies-in-custom-class/147707/1 "2008-09-04T13:50:32Z")

</div>

Hello!

I have created a custom class in my RoR application and put it into lib/  
folder.  
How can I access cookies and session variables in this class? I need to  
set and read variables from them.

Best regards.

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [September 4, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/cookies-in-custom-class/147707/2 "2008-09-04T13:52:17Z")

</div>

On 4 Sep 2008, at 13:49, Konstantin wrote:

> Hello!
> 
> I have created a custom class in my RoR application and put it into  
> lib/  
> folder.  
> How can I access cookies and session variables in this class? I need  
> to  
> set and read variables from them.

You can’t sensibly. cookies and session are instance methods of  
controllers.

Fred

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [September 4, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/cookies-in-custom-class/147707/3 "2008-09-04T14:25:06Z")

</div>

Maybe I can inherit my class from controller?  
I really need to make a stand alone class to operate with session and  
cookie variables

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [September 4, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/cookies-in-custom-class/147707/4 "2008-09-04T14:38:39Z")

</div>

On 4 Sep 2008, at 14:24, Konstantin wrote:

> Maybe I can inherit my class from controller?  
> wouldn’t make any difference - you wouldn’t be that instance of the  
> controller.
> 
> I really need to make a stand alone class to operate with session and  
> cookie variables

Just pass session/cookie parameters through when you call stuff in  
your magic.

if you have to, have a look at  
[http://m.onkey.org/2007/10/17/how-to-access-session-cookies-params-request-in-model](http://m.onkey.org/2007/10/17/how-to-access-session-cookies-params-request-in-model)

Fred
