# Layout Question w/ code from vendor library

**URL:** <https://www.ruby-forum.com/t/layout-question-w-code-from-vendor-library/56701>\
**Category:** Rails\
**Created:** [April 3, 2006, 3:52am UTC](https://www.ruby-forum.com/t/layout-question-w-code-from-vendor-library/56701 "2006-04-03T03:52:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 3, 2006, 3:52am UTC](https://www.ruby-forum.com/t/layout-question-w-code-from-vendor-library/56701/1 "2006-04-03T03:52:55Z")

</div>

I am new to Ruby on Rails in general and apologize if this question is  
trivial and/or has been asked before ( my searches did not turn up any  
threads ).

I am using the cookies function from actionpack which I have located in  
my vendor/rails/actionpack directory.

Using:

cookies[:username] and cookies[:password]

from within a controller file works just fine, I am able to set and read  
cookies.

I would like to add cookie functionality to the overall template that my  
controllers are using via layout “template”, however, trying to access  
cookies[] causes the following error:

* * *

## undefined local variable or method `cookies’ for #\<#Class:0x40a87040:0x40a86d0c\>

I am missing something as I suspect that I should be able to access the  
external libs I have stored in vendor but I cannot put my finger on what  
I need to do.

Any help would be greatly appreciated!

-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 3, 2006, 4:12am UTC](https://www.ruby-forum.com/t/layout-question-w-code-from-vendor-library/56701/2 "2006-04-03T04:12:45Z")

</div>

Hello Andrew,

2006/4/2, Andrew C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I would like to add cookie functionality to the overall template that my  
> controllers are using via layout “template”, however, trying to access  
> cookies[] causes the following error:
> 
> * * *
> 
> ## undefined local variable or method `cookies’ for #\<#Class:0x40a87040:0x40a86d0c\>

You should not be accessing cookies from your views. Instead, the  
controller should make any necessary values available to the view.  
The view shouldn’t need to know where the values come from.

That being said, you can probably use @cookies in the view, if you  
really want. Haven’t tried it.

Bye !

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 3, 2006, 5:59am UTC](https://www.ruby-forum.com/t/layout-question-w-code-from-vendor-library/56701/3 "2006-04-03T05:59:27Z")

</div>

Why not save this kind of information in the session.

The session usually tracks which user is currently logged in for the  
session  
in the various user management plugins.

then in your view or controller  
session[‘user’]

There are many systems that do this, although they usually provide a  
helper  
method for the views.

ie  
def current\_user  
session[‘user’]  
end

then in your view call curren\_user

The acts\_as\_authenticated plugin is a good place to start for user  
logins  
and such.

Cheers

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 3, 2006, 6:40am UTC](https://www.ruby-forum.com/t/layout-question-w-code-from-vendor-library/56701/4 "2006-04-03T06:40:56Z")

</div>

Thank you, I will look into this solution!

🙂

-Andy

Daniel ----- wrote:

> Why not save this kind of information in the session.
> 
> The session usually tracks which user is currently logged in for the  
> session  
> in the various user management plugins.
> 
> then in your view or controller  
> session[‘user’]
> 
> There are many systems that do this, although they usually provide a  
> helper  
> method for the views.
> 
> ie  
> def current\_user  
> session[‘user’]  
> end
> 
> then in your view call curren\_user
> 
> The acts\_as\_authenticated plugin is a good place to start for user  
> logins  
> and such.
> 
> Cheers

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 3, 2006, 5:09am UTC](https://www.ruby-forum.com/t/layout-question-w-code-from-vendor-library/56701/5 "2006-04-03T05:09:16Z")

</div>

Well, for example if I want to check the ‘logged\_in’ status of a user  
based on cookied username/password and display a message such as:

“Welcome back (username)”

(or)

“You are not logged in, please login here”

it makes sense to me that such a message should be provided in the  
overall template specified by the layout, rather than have every  
action’s view format the message and display it.

Is there a way to avoid having to re-use the code to check login state  
and prepare the message for every specific view ?

Thanks  
-Andy

FranÃ§ois Beausoleil wrote:

> Hello Andrew,
> 
> 2006/4/2, Andrew C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > I would like to add cookie functionality to the overall template that my  
> > controllers are using via layout “template”, however, trying to access  
> > cookies[] causes the following error:
> > 
> > * * *
> > 
> > ## undefined local variable or method `cookies’ for #\<#Class:0x40a87040:0x40a86d0c\>
> 
> You should not be accessing cookies from your views. Instead, the  
> controller should make any necessary values available to the view.  
> The view shouldn’t need to know where the values come from.
> 
> That being said, you can probably use @cookies in the view, if you  
> really want. Haven’t tried it.
> 
> Bye !
