HI, I have inherited support on a rails website and I am encountering the following problem: <% if @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'')!=nil %> <%= stylesheet_link_tag ''ie6'' %> <% elsif @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 7.0'')!=nil %> <%= stylesheet_link_tag ''ie7'' %> <% else %> <%= stylesheet_link_tag ''moz'' %> <% end %> this little code snippet works except when the user agent string is empty in which case i get : (undefined method `downcase'' for nil:NilClass) how can i edit this code so that it can handle an empty user agent string? thanks -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Feb 8, 2010 at 12:53 PM, Ketema Harris <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> HI, I have inherited support on a rails website and I am encountering > the following problem: > > <% if @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'')!=nil %> > <%= stylesheet_link_tag ''ie6'' %> > <% elsif > @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 7.0'')!=nil %> > <%= stylesheet_link_tag ''ie7'' %> > <% else %> > <%= stylesheet_link_tag ''moz'' %> > <% end %> > this little code snippet works except when the user agent string is > empty in which case i get : (undefined method `downcase'' for > nil:NilClass) > > how can i edit this code so that it can handle an empty user agent > string?@agent = @request.env[''HTTP_USER_AGENT''] rescue nil <% if @agent -%> ... <% end -%> -- Greg Donald destiney.com | gregdonald.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ketema Harris wrote:> HI, I have inherited support on a rails website and I am encountering > the following problem: > > <% if @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'')!=nil %> > <%= stylesheet_link_tag ''ie6'' %> > <% elsif > @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 7.0'')!=nil %> > <%= stylesheet_link_tag ''ie7'' %> > <% else %> > <%= stylesheet_link_tag ''moz'' %> > <% end %> > this little code snippet works except when the user agent string is > empty in which case i get : (undefined method `downcase'' for > nil:NilClass) > > how can i edit this code so that it can handle an empty user agent > string? > > thanksIntroduce a call to nil? . No problem. And for goodness'' sakes, get this code out of the view file. Logic doesn''t belong there. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ketema Harris wrote:> HI, I have inherited support on a rails website and I am encountering > the following problem: > > <% if @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'')!=nil %> > <%= stylesheet_link_tag ''ie6'' %>If the user agent is for IE6...> <% elsif > @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 7.0'')!=nil %> > <%= stylesheet_link_tag ''ie7'' %>If the user agent is for IE7...> <% else %> > <%= stylesheet_link_tag ''moz'' %>If the user agent is anything else, including empty ...> <% end %> > this little code snippet works except when the user agent string is > empty in which case i get : (undefined method `downcase'' for > nil:NilClass) > > how can i edit this code so that it can handle an empty user agent > string? >Ah, I see. You want ''nil'' .. So you should first say that : if @request.env[''HTTP_USER_AGENT''].nil? .. And then whatever you want to do. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Katerna, Rails has a handy helper for this. blank? returns true for false, empty or whitespace strings. nil is false. You''ll need to move the negation to the beginning. On Mon, 2010-02-08 at 19:53 +0100, Ketema Harris wrote:> % if @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'')!=nilif !@request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'').blank? HTH, Bill -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 8, 2010, at 2:13 PM, Aldric Giacomoni wrote:> Ketema Harris wrote: >> HI, I have inherited support on a rails website and I am encountering >> the following problem: >> >> <% if @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'')! >> =nil %> >> <%= stylesheet_link_tag ''ie6'' %> > If the user agent is for IE6... > >> <% elsif >> @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 7.0'')!=nil %> >> <%= stylesheet_link_tag ''ie7'' %> > If the user agent is for IE7... > >> <% else %> >> <%= stylesheet_link_tag ''moz'' %> > If the user agent is anything else, including empty ... > >> <% end %> >> this little code snippet works except when the user agent string is >> empty in which case i get : (undefined method `downcase'' for >> nil:NilClass) >> >> how can i edit this code so that it can handle an empty user agent >> string? >> > > Ah, I see. You want ''nil'' .. So you should first say that : > if @request.env[''HTTP_USER_AGENT''].nil? > .. And then whatever you want to do.Start with something like: user_agent = @request.env.fetch(''HTTP_USER_AGENT'', ''empty'').downcase Then replace all the other tests: if user_agent.index(''msie 6.0'') There''s no need to say !=nil (but I agree that you should get the logic out of the view if possible. Perhaps in a helper? def user_agent @request.env.fetch(''HTTP_USER_AGENT'', ''empty'').downcase end def browser_specific_stylesheet case user_agent when /msie 6.0/ ''ie6'' when /msie 7.0/ ''ie7'' else ''moz'' end end <%= stylesheet_tag browser_specific_stylesheet %> -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn wrote:> On Feb 8, 2010, at 2:13 PM, Aldric Giacomoni wrote: >>> @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 7.0'')!=nil %> >>> nil:NilClass) >>> >>> how can i edit this code so that it can handle an empty user agent >>> string? >>> >> >> Ah, I see. You want ''nil'' .. So you should first say that : >> if @request.env[''HTTP_USER_AGENT''].nil? >> .. And then whatever you want to do. > > Start with something like: > > user_agent = @request.env.fetch(''HTTP_USER_AGENT'', ''empty'').downcase > > Then replace all the other tests: > > if user_agent.index(''msie 6.0'') > > There''s no need to say !=nil > > (but I agree that you should get the logic out of the view if > possible. Perhaps in a helper? > > def user_agent > @request.env.fetch(''HTTP_USER_AGENT'', ''empty'').downcase > end > > def browser_specific_stylesheet > case user_agent > when /msie 6.0/ > ''ie6'' > when /msie 7.0/ > ''ie7'' > else > ''moz'' > end > end > > <%= stylesheet_tag browser_specific_stylesheet %> > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgthanks for all the replies! i got a check to work for all cases: <% if !@request.env[''HTTP_USER_AGENT''].nil? if @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 6.0'')!=nil %> <%= stylesheet_link_tag ''ie6'' %> <% elsif @request.env[''HTTP_USER_AGENT''].downcase.index(''msie 7.0'')!=nil %> <%= stylesheet_link_tag ''ie7'' %> <% else %> <%= stylesheet_link_tag ''moz'' %> <% end %> <% end %> not the prettiest code, but its working and no more error thanks again -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 8, 2:52 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> > (but I agree that you should get the logic out of the view if > possible. Perhaps in a helper? >He should get the logic out of the app *entirely*. This is the exact use case for IE''s conditional comments: http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx --Matt Jones -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.