Layout Question w/ code from vendor library


#1

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


#2

Hello Andrew,

2006/4/2, Andrew C. 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 !


#3

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


#4

Thank you, I will look into this solution!

:slight_smile:

-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


#5

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:

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 !