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?
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
Introduce a call to nil? . No problem.
And for goodness' sakes, get this code out of the view file. Logic
doesn't belong there.
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.
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.
thanks 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