Forum: Ruby on Rails Layout Question w/ code from vendor library

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af1ee2208795752559b049502ded660c?d=identicon&s=25 Andrew Cowan (k2mia)
on 2006-04-03 03:52
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
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-04-03 04:12
(Received via mailing list)
Hello Andrew,

2006/4/2, Andrew Cowan <icculus@gmdstudios.com>:
> 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 !
Af1ee2208795752559b049502ded660c?d=identicon&s=25 Andrew Cowan (k2mia)
on 2006-04-03 05:09
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 Cowan <icculus@gmdstudios.com>:
>> 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 !
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-04-03 05:59
(Received via mailing list)
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
Af1ee2208795752559b049502ded660c?d=identicon&s=25 Andrew Cowan (k2mia)
on 2006-04-03 06:40
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
This topic is locked and can not be replied to.