nulltek
nulltek

Reputation: 3337

Rails bootstrap-datepicker-rails not working

I have a Rails 3.2.19 app using twitter-bootstrap-rails 2.2.6 and bootstrap-datepicker-rails 1.3.0.2.

I am trying to get a text_field in a form to display the datepicker but nothing happens when I click or focus on the field.

When I inspect the element I see the following:

<input data-behaviour="datepicker" id="project_due_date" name="project[due_date]" size="30" type="text">

When checking the console I do not receive any JS/Coffee errors.

Here is my code:

projects.js.coffee

 $(document).on "focus", "[data-behaviour~='datepicker']", (e) ->
 - $(this).datepicker
 - format: "yyyy-mm-dd"
 - weekStart: 1
 - autoclose: true

projects/_form.html.erb

<%= form_for(@project, :html => { :class => "well"}) do |f|  %>
 <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this contact from being created:</h2>

      <ul>
      <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.label :project_name %>
  <%= f.text_field :project_name, placeholder: "Build Spreadsheet", required: true %>
  <%= f.label :project_manager %>
  <%= f.collection_select(:user_id, User.all, :id, :full_name)%>
  <%= f.label :due_date %>
  <%= f.text_field :due_date, 'data-behaviour' => 'datepicker' %>
  </br>
<%= f.button :submit, :class => 'btn btn-info' %>
  <% end %>

I've tried also calling it based on id and have had no luck. I see the same behavior in Chrome, Firefox, or Safari.

Any help is greatly appreciated.

Upvotes: 0

Views: 665

Answers (1)

nulltek
nulltek

Reputation: 3337

It looks like my CoffeeScript was wrong. I changed it to the following:

$ ->
  $('[data-behaviour~=datepicker]').datepicker(format: "yyyy-mm-dd", autoclose: true)

Now I'm able to select the field and the datepicker comes up.

Upvotes: 1

Related Questions