(this article was writen in 2016, so this can be obsolete now)
I wrote this article to explain how to install a datetimepicker in a rails project with bootstrap 4. Indeed, I spent a lot of time to find the solution, because it worked only with bootstrap 3.
A datetimepicker is essential to have a much better user experience in your forms.
Here, is what I did to install it :
bootstrap-datetimepicker Don’t install the gem !
Install moment for the format of dates.
gem 'momentjs-rails', '>= 2.9.0
//= require moment
Don’t forget to re run your server
Add the file bootstrap-datetimepicker.js and copy past the content of the file from github project src.
bootstrap-datetimepicker/src/js/bootstrap-datetimepicker.js
//= require bootstrap-datetimepicker.js
Add the file bootstrap-datetimepicker.css and copy past the content of build folder in github project
bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.css
*= require bootstrap-datetimepicker
Now the install is done, re run your server.
If you are using simple form for, add this code in your input:
as: :string, input_html: { class:"datetimepicker" }
The class “datetimepicker” that it will be called in the script
<script type="text/javascript">
$('.datetimepicker').datetimepicker({
format: "YYYY-MM-DD";
});
</script>
Now it is work ! but the icons are not appear, indeed, because of the new version of bootstrap, you have to replace some code in your js file.
Replace this code :
icons: {
time: 'glyphicon glyphicon-time',
date: 'glyphicon glyphicon-calendar',
up: 'glyphicon glyphicon-chevron-up',
down: 'glyphicon glyphicon-chevron-down',
previous: 'glyphicon glyphicon-chevron-left',
next: 'glyphicon glyphicon-chevron-right',
today: 'glyphicon glyphicon-screenshot',
clear: 'glyphicon glyphicon-trash',
close: 'glyphicon glyphicon-remove'
}
By font-awesome icons:
icons: {
time: 'fa fa-clock-o',
date: 'fa fa-calendar',
up: 'fa fa-chevron-up',
down: 'fa fa-chevron-down',
previous: 'fa fa-chevron-left',
next: 'fa fa-chevron-right',
today: 'glyphicon glyphicon-screenshot',
clear: 'fa fa-trash',
close: 'fa fa-times'
}
Great ! it is working
I hope my article will help you!