Last Updated on November 30, 2019 by Christopher G Mendla
I was using the date_select for entering dates in Rails apps. For most browsers such as Chrome and Firefox, it works perfectly. The user can use a mini pop up calendar to select the date.
I was going to write some browser sniffing Javascript code to switch the date inputs based on the browser. However, I found a simple, elegant solution called browser. All you need to do is install it in the gemfile and require it in the application controller.
https://github.com/fnando/browser
The browser gem allows you to do things like ‘if browser.ie?’ plus a LOT more.
Since the application I am currently developing is behind a firewall with a limited number of machines and browsers, the following works fine in the view (The date picker is in the view to change the date range of the report. )
<% if browser.ie? then %>
From Date: <%= date_select :from, :post %>
To Date:<%= date_select :to,:post %>
<% else %>
From Date: <%= date_field_tag ‘from’ %>
To Date: <%= date_field_tag ‘to’ %>
<% end %>
With the code above, the limited date_select will show if the user is running IE and the better date_field_tag will show for other browsers.