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
[email protected]
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.