1

I'm trying to get a datepicker (using bootstrap-datepicker-rails gem) to display via simple_form with bootstrap modal, but having no luck.

I've read a bunch of stuff such as:

https://github.com/Nerian/bootstrap-datepicker-rails/issues/51 How to setup bootstrap-datepicker-rails? http://learningnewtricks.wordpress.com/2013/07/03/bootstrap-date-picker-on-rails-so-simple/ http://www.chadcf.com/blog/jqueryui-datepicker-rails-and-simpleform-easy-way

All I get is a textfield but when I click on it there is no datepicker. Could it be the datepicker is behind the modal, if so how to fix that?

My setup is as follows:

fyousuf$ rails -v
Rails 3.2.12

fyousuf$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]

app/assets/stylesheets/application.css

    *= require_self
    *= require fullcalendar
    *= require bootstrap-datepicker
    *= require_tree .

app/assets/javascripts/application.js

    //= require jquery
    //= require jquery_ujs
    //= require twitter/bootstrap
    //= require bootstrap
    //= require fullcalendar
    //= require bootstrap-datepicker
    //= require_tree .

app/assets/javascript/s/events.js

    # Place all the behaviors and hooks related to the matching controller here.
    # All this logic will automatically be available in application.js.
    # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

    $('[data-behaviour~=datepicker]').datepicker({"format": "yyyy-mm-dd", "weekStart": 1,      "autoclose": true});

app/views/events/_form.html.erb

    <%= simple_form_for(@event) do |f| %>
        ...
        <%= f.input :starts_at, :as => :string, :input_html => { data: {behaviour: "datepicker"}} %>
        ...
    <% end %>
Community
  • 1
  • 1
Farooq
  • 1,925
  • 3
  • 15
  • 36
  • Wrap the `datepicker` initialization code in `events.js` within jquery document ready, i.e. `$(document).ready(function(){/* Your datepicker initialization code here */});` – vee Dec 09 '13 at 06:51
  • Changed to this but still same behavior: $(document).ready(function() { $('[data-behaviour~=datepicker]').datepicker({"format": "yyyy-mm-dd", "weekStart": 1, "autoclose": true}); }); – Farooq Dec 09 '13 at 14:35

1 Answers1

3

On my view

= f.input :first_name
= f.input :last_name
= f.input :date_of_birth, as: :string, input_html: { class: "datepicker" }

On my application.js

$(function() {
    $('input.datepicker').data({behaviour: "datepicker"}).datepicker();
});

The versions that I'm using

> bundle | grep bootstrap

Using bootstrap-datepicker-rails 1.3.0.2
Using twitter-bootstrap-rails 2.2.8

It's perfectly working for me. Right after the form is loaded if I click on the input "date_of_birth" the datepicker appears.

Emiliano Zilocchi
  • 1,075
  • 1
  • 8
  • 12