How to setup bootstrap-datepicker-rails?

21,950

Solution 1

Look at step 5 and step 6, your selection class .datepicker in javascript but not in input field

Try this

on views (e.g form)

<%= f.text_field :mydate, 'data-behaviour' => 'datepicker' %>

<script type="text/javascript">
  $('[data-behaviour~=datepicker]').datepicker();
</script>

Or As component :

<div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
    <%= f.text_field :mydate, 'data-behaviour' => 'datepicker', :disabled => 'disable' %><span class="add-on"><i class="icon-th"></i></span>
</div>

<script type="text/javascript">
  $('.datepicker').datepicker();
</script>

Example above all if the attribute using date type. You can use bootstrap-datetimepicker-rails gem, if the attribute using datetime type

Solution 2

In the view, try this:
<div class="field">
  <%= f.label 'Trade Date: ' %>
  <%= f.text_field :trade_date, 'data-behaviour' => 'datepicker' %><br />
</div>  
In your application.js, use:
$(document).on("focus", "[data-behaviour~='datepicker']", function(e){
    $(this).datepicker({"format": "yyyy-mm-dd", "weekStart": 1, "autoclose": true})
});
instead of 
$('.datepicker').datepicker()
Share:
21,950
Andres Calle
Author by

Andres Calle

Gerente en TRESCLOUD

Updated on July 09, 2022

Comments

  • Andres Calle
    Andres Calle almost 2 years

    Anybody knows how to setup the gem bootstrap-datepicker-rails? I followed the steps in http://rubydoc.info/gems/bootstrap-datepicker-rails/0.6.21/frames, basically:

    1. I setup twitter-bootstrap-rails gem
    2. I add this to gemfile gem 'bootstrap-datepicker-rails', '>= 0.6.21'
    3. Add this line to app/assets/stylesheets/application.css

      *= require bootstrap-datepicker
      
    4. Add this line to app/assets/javascripts/application.js

      //= require bootstrap-datepicker
      
    5. Add this line to app/assets/javascripts/controllername.js.coffee

      $('.datepicker').datepicker()
      
    6. Finally use in view :)

      <input type="text" data-behaviour='datepicker' >
      

    But it doesn't work!! Anybody has got it to work? It should work nice as shown here:
    http://jsfiddle.net/2Gg5k/43/

    • shicholas
      shicholas over 11 years
      i have the same problem, have you found a solution? I keep getting an uncaught exemption that datepicker is not defined.
  • Htoo Myat Aung
    Htoo Myat Aung over 10 years
    how about for rails text field tag which is like <%= text_field_tag "test", nil, :tabindex => '3', data: { behavior: "datepicker" }, :value =>Time.now.strftime("%Y-%m-%d"), :id => "datepicker" %>