Search code examples
ruby-on-railstwitter-bootstraptwitter-bootstrap-rails

Twitter Bootstrap Rails Datetime picker


I would like to include the following datetime picker in my rails application.

http://www.malot.fr/bootstrap-datetimepicker/

I am using twitter bootstrap for rails, but can't get it to work.

I tried adding the .js file to the assets/javascripts directory and using //= require to include it in the application.js file.

The files are loading on the page, but I'm getting a response that

Uncaught TypeError: Object [object Object] has no method 'datetimepicker'

Any ideas? I've reverted all of my changes, so can start again from scratch.

application.js

//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require bootstrap
//= require_tree .

Solution

  • I think the problem happened at //=require_tree. Yes you added that plugin but the plugin is loaded after your custom js which have method to call the plugin. That's why "Object has no method 'datetimepicker'".

    I would suggest you to:

    1. Move the plugin into vendor/assets/javascripts/. This is a better place for third party libs.

    2. require the plugin explicitly in application, after bootstrap(it depends on bootstrap's dropdown. Like

      //= require bootstrap
      //= require datetime_picker_js_file_name
      //= require_tree .
      

    Side notes:

    I don't quite know why there are two bootstrap js files required. Are they duplicate?

    Besides, I would recommend to require Bootstrap js files only on need, like

    //= require bootstrap-dropdown
    //= require bootstrap-alert