November 16

0 comments

Date Pickers in Rails (Internet Explorer and other browsers)

By Christopher Mendla

November 16, 2016


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.

 
However, if you use a date_select and the user is using Internet Explorer, they just end up with a plain input box.
 
You can use a date_field_tag  which will give the user a less useable date picker that will work in all browsers.
 
 

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.

Christopher Mendla

About the author

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}