Rails 5 - Using bootstrap datepicker unless browser supports HTML5 date controls

自古美人都是妖i 提交于 2019-12-07 09:51:37

using date field in rails with bootstrap, I would suggest as follow

here is the Gemffile

gem 'jquery-ui-rails', '~> 4.2.1'

inside application.js (put below jquery)

//= require jquery.ui.datepicker

and in your date field just use the class with datepicker, below is the sample, I also add additional code in case your want to edit the date field with f.object

<%= f.text_field :your_field, :class => 'form-control datepicker', :value=>"#{f.object.your_field.strftime("%m-%d-%Y") unless f.object.new_record? or f.object.your_field.nil?}" %>  %>

create one file in config/initializers/date_formats.rb

Date::DATE_FORMATS[:default] = "%m-%d-%Y"

don't forget to restart your rails server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!