comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-07 16:44 UTC
Read cookies in application_helper.rb?
Hi there, I am trying to write a simple function that checks if a user is logged in based on a cookie value. def isLoggedIn() if cookies[:userid] != "" isLoggedIn = true else isLoggedIn = false end end however, when I call this function from my application-wide view, I get the following error: undefined local variable or method `cookies'' for #<#<Class:0xb6e0e920>:0xb6e0e86c> Can anyone help me understand how I can access my cookies from anywhere other than an action in a controller? Thanks, Bryan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi there, > > I am trying to write a simple function that checks if a user is logged > in based on a cookie value. > > def isLoggedIn() > > if cookies[:userid] != "" > isLoggedIn = true > else > isLoggedIn = false > end > > end > > however, when I call this function from my application-wide view, I get > the following error: > > undefined local variable or method `cookies'' for > #<#<Class:0xb6e0e920>:0xb6e0e86c> > > Can anyone help me understand how I can access my cookies from anywhere > other than an action in a controller? > > Thanks, > > BryanWhat you are wanting is to use the session hash. So something like session[:blah] . This way the cookie is only a pointer to the session. Never trust storing important data in a cookie since it can be modified. Do a bit of reading up on the session hash. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-07 16:58 UTC
Re: Read cookies in application_helper.rb?
But how long is a session active for in Rails? Is ASP it''s good for 20 minutes. I don''t want the user to have to log in every time they come to the site. So I wouldn''t think a session variable is the way to go. Let''s table the discussion of cookie vs session. Where am I allowed to read cookies and why? Bry> > What you are wanting is to use the session hash. So something like > session[:blah] . This way the cookie is only a pointer to the session. > Never trust storing important data in a cookie since it can be modified. > > Do a bit of reading up on the session hash. > > > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I am trying to write a simple function that checks if a user is logged > in based on a cookie value. > > def isLoggedIn() > > if cookies[:userid] != "" > isLoggedIn = true > else > isLoggedIn = false > end > > end > > however, when I call this function from my application-wide view, I get > the following error: > > undefined local variable or method `cookies'' for > #<#<Class:0xb6e0e920>:0xb6e0e86c> > > Can anyone help me understand how I can access my cookies from anywhere > other than an action in a controller?Add to your ApplicationController class (in application.rb) helper_method :cookies -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-07 18:23 UTC
Re: Read cookies in application_helper.rb?
Awesome! This worked like a charm. Thank you. Now, I have another problem. My function is always returning true. Even if I change it and force it return something else. For example: def isLoggedIn() isLoggedIn = "bob" end so when I do this: <%= isLoggedIn %> I am seeing: true How do I get my function to actually return "bob" Bry> Add to your ApplicationController class (in application.rb) > > helper_method :cookies > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Why not just do something like this
def isLoggedIn()
cookies[:userid] ? true : false
end
On Nov 7, 1:23 pm,
"comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Awesome! This worked like a charm. Thank you.
>
> Now, I have another problem. My function is always returning true.
> Even if I change it and force it return something else. For example:
>
> def isLoggedIn()
>
> isLoggedIn = "bob"
>
> end
>
> so when I do this:
>
> <%= isLoggedIn %>
>
> I am seeing: true
>
> How do I get my function to actually return "bob"
>
> Bry
>
> > Add to your ApplicationController class (in application.rb)
>
> > helper_method :cookies
>
> > --
> > We develop, watch us RoR, in numbers too big to ignore.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I don''t understand that syntax at all? Question mark? colon? Also, when I try to call a function in my view <% if isLoggedIn() =true %> I receive an "undefined method" error. Bry On 11/7/06, Dr J <cpjolicoeur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Why not just do something like this > > def isLoggedIn() > > cookies[:userid] ? true : false > > end > > On Nov 7, 1:23 pm, "comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" > <comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Awesome! This worked like a charm. Thank you. > > > > Now, I have another problem. My function is always returning true. > > Even if I change it and force it return something else. For example: > > > > def isLoggedIn() > > > > isLoggedIn = "bob" > > > > end > > > > so when I do this: > > > > <%= isLoggedIn %> > > > > I am seeing: true > > > > How do I get my function to actually return "bob" > > > > Bry > > > > > Add to your ApplicationController class (in application.rb) > > > > > helper_method :cookies > > > > > -- > > > We develop, watch us RoR, in numbers too big to ignore. > > > > >-- Comic Geek Speak Uniting the world''s mightiest heroes, one listener at a time. http://www.comicgeekspeak.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
To make it easier for your to read try this
def isLoggedIn()
return true unless cookies[:userid].nil?
return false
end
that should return true only if the cookie[:userid] value is set.
then in your view just do <% if isLoggedIn() %>
you dont need <% if isLoggedIn() == true %> since isLoggedIn() is by
nature either true or false.
On Nov 7, 4:13 pm, "Comic Geek Speak"
<comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I don''t understand that syntax at all? Question mark? colon?
>
> Also, when I try to call a function in my view <% if isLoggedIn() =>
true %> I receive an "undefined method" error.
>
> Bry
>
> On 11/7/06, Dr J
<cpjolico...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
>
>
>
> > Why not just do something like this
>
> > def isLoggedIn()
>
> > cookies[:userid] ? true : false
>
> > end
>
> > On Nov 7, 1:23 pm,
"comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
> > <comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > Awesome! This worked like a charm. Thank you.
>
> > > Now, I have another problem. My function is always returning
true.
> > > Even if I change it and force it return something else. For
example:
>
> > > def isLoggedIn()
>
> > > isLoggedIn = "bob"
>
> > > end
>
> > > so when I do this:
>
> > > <%= isLoggedIn %>
>
> > > I am seeing: true
>
> > > How do I get my function to actually return "bob"
>
> > > Bry
>
> > > > Add to your ApplicationController class (in application.rb)
>
> > > > helper_method :cookies
>
> > > > --
> > > > We develop, watch us RoR, in numbers too big to ignore.--
> Comic Geek Speak
> Uniting the world''s mightiest heroes, one listener at a time.
>
> http://www.comicgeekspeak.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
btw the tertiary operator ( ? : ) is just a more succinct way of doing an if .. then .. else statement On Nov 7, 4:13 pm, "Comic Geek Speak" <comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I don''t understand that syntax at all? Question mark? colon? > > Also, when I try to call a function in my view <% if isLoggedIn() => true %> I receive an "undefined method" error. > > Bry > > On 11/7/06, Dr J <cpjolico...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Why not just do something like this > > > def isLoggedIn() > > > cookies[:userid] ? true : false > > > end > > > On Nov 7, 1:23 pm, "comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" > > <comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Awesome! This worked like a charm. Thank you. > > > > Now, I have another problem. My function is always returning true. > > > Even if I change it and force it return something else. For example: > > > > def isLoggedIn() > > > > isLoggedIn = "bob" > > > > end > > > > so when I do this: > > > > <%= isLoggedIn %> > > > > I am seeing: true > > > > How do I get my function to actually return "bob" > > > > Bry > > > > > Add to your ApplicationController class (in application.rb) > > > > > helper_method :cookies > > > > > -- > > > > We develop, watch us RoR, in numbers too big to ignore.-- > Comic Geek Speak > Uniting the world''s mightiest heroes, one listener at a time. > > http://www.comicgeekspeak.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11/7/06, Comic Geek Speak <comicgeekspeak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I don''t understand that syntax at all? Question mark? colon?It''s called a ternary operator. The expression before the ? is evaluated, and if true whatever is between the ? and : is returned. If the expression before ? is false, then whatever is after the : is returned. Chris --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
my mistake, you are right. its the "ternary" not the "tertiary" operator. don''t know what i was thinking of. haha On Nov 7, 4:39 pm, snacktime <snackt...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 11/7/06, Comic Geek Speak <comicgeeksp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I don''t understand that syntax at all? Question mark? colon?It''s called a ternary operator. The expression before the ? is > evaluated, and if true whatever is between the ? and : is returned. > If the expression before ? is false, then whatever is after the : is > returned. > > Chris--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---