I''m working on a ''simple'' style switcher for a website and running into problems detecting if a cookie exists in a helper.. the offending code is .. def zoomed? @cookies["layout"].value.to_s == ''zoom'' unless @cookies["layout"].nil? end if no cookie exists the app crashes with a "undefined method `value'' for []:Array", so how can I safely check if the cookie exists? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hello Kevin, kevin evans said the following on 2005-09-06 08:47:> I''m working on a ''simple'' style switcher for a website and running into > problems detecting if a cookie exists in a helper.. > > the offending code is .. > > def zoomed? > @cookies["layout"].value.to_s == ''zoom'' unless > @cookies["layout"].nil? > end > > if no cookie exists the app crashes with a "undefined method `value'' for > []:Array", so how can I safely check if the cookie exists?if cookies.has_key?(:layout) then true if cookies[:layout] == ''zoom'' else false end NOTE: I am using the accessor methods, not the instance variables. Also, I use symbols and not strings, as newer Rails app should be using symbols. Symbols use less memory - one reference instead of another object for each instance. In the end though, that is probably not your problem. I am guessing that the object returned by cookies[:layout] is not a Cookie instance. Add some logging to this method, as in: def zoomed?() logger.debug cookies.inspect logger.debug cookies[:layout].inspect cookies[:layout].blank? ? false : cookies[:layout].value == ''zoom'' end That should do the trick ! Bye ! François
Hi François alas, no luck. Using cookies[:layout] in a helper just crashes with undefined local variable or method `cookies'' for #<ActionView::Base:0x255910c> so, if I use the code.. def zoomed? logger.debug "One:: "+ cookies.inspect logger.debug "Two:: "+cookies[:layout].inspect cookies[:layout].blank? ? false : cookies[:layout].value == ''zoom'' end gives .. One:: {"_session_id"=>["8f06eb6c4ab5a6d7e853dc55ef099b9b"], "author"=>["kwe"], "layout"=>["zoom"]} Two:: [] The first option - changed to use @cookies, always evals to false. btw: the cookie is being set ok, just frustrating to get this helper to work..>Hello Kevin,kevin evans said the following on 2005-09-06 08:47:> I''m working on a ''simple'' style switcher for a website and running into > problems detecting if a cookie exists in a helper.. > > the offending code is .. > > def zoomed? > @cookies["layout"].value.to_s == ''zoom'' unless > @cookies["layout"].nil? > end > > if no cookie exists the app crashes with a "undefined method `value'' for > []:Array", so how can I safely check if the cookie exists? >.if cookies.has_key?(:layout) then> true if cookies[:layout] == ''zoom'' >else > false >end>NOTE: I am using the accessor methods, not the instance variables. >Also, I use symbols and not strings, as newer Rails app should be using >symbols. Symbols use less memory - one reference instead of another >object for each instance.>In the end though, that is probably not your problem. I am guessing >that the object returned by cookies[:layout] is not a Cookie instance. >Add some logging to this method, as in: >def zoomed?() > logger.debug cookies.inspect > logger.debug cookies[:layout].inspect > cookies[:layout].blank? ? false : cookies[:layout].value == ''zoom'' >end>That should do the trick !>Bye ! >François_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
And what about @cookies["layout"].value.to_s == 'zoom' unless @cookies["layout"].nil? || @cookies['layout'].empty? but maybe I didn't get it. On 9/6/05, kevin evans <kwevans@gmail.com> wrote:> I'm working on a 'simple' style switcher for a website and running into > problems detecting if a cookie exists in a helper.. > > the offending code is .. > > def zoomed? > @cookies["layout"].value.to_s == 'zoom' unless > @cookies["layout"].nil? > end > > if no cookie exists the app crashes with a "undefined method `value' for > []:Array", so how can I safely check if the cookie exists? > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ok, got it working now.. def zoomed? @cookies["layout"].value.to_s == ''zoom'' if @cookies.has_key?("layout") end seems to do the trick. thanks all Kevin>And what about> @cookies["layout"].value.to_s == ''zoom'' unless >@cookies["layout"].nil? || @cookies[''layout''].empty?>but maybe I didn''t get it.On 9/6/05, kevin evans <kwevans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m working on a ''simple'' style switcher for a website and running into > problems detecting if a cookie exists in a helper.. > > the offending code is .. > > def zoomed? > @cookies["layout"].value.to_s == ''zoom'' unless > @cookies["layout"].nil? > end > > if no cookie exists the app crashes with a "undefined method `value'' for > []:Array", so how can I safely check if the cookie exists? > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >