Hrittik
Hrittik

Reputation: 1

bootstrap datepicker rails gem is not working

I don't understand why the datepicker gem is not working. Here is the code of my app:

Here is my gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.3'
# Use sqlite3 as the database for Active Record
gem 'devise'
gem 'milia', github: 'jekuno/milia', ref: '05b72dd'
gem 'aws-sdk'
gem 'stripe'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
gem 'jquery-rails'
gem 'twitter-bootstrap-rails'
gem 'devise-bootstrap-views'
gem 'bootstrap-datepicker-rails'

gem 'certified'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  gem 'sqlite3'
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'activerecord-session_store', github: 'rails/activerecord-session_store'

Here is my application.css file:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
 * vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope.
 *
 *= require devise_bootstrap_views
 *= require bootstrap-datepicker3
 *= require_tree .
 *= require_self
 */

Here is my application.js file:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require bootstrap-datepicker
//= require turbolinks
//= require_tree .

Here is my datepicker.js file:

$(document).ready(function(){

    $(function() {

        $('.datepicker').datepicker({format: 'yyyy-mm-dd'});

    });

});

Here is the snippet of my _form.html.erb file:

</div>
  <div class="form-group">
    <%= f.label :expected_completion_date, :class => 'control-label col-lg-2' %>
    <div class="col-lg-10">
      <%= f.text_field :expected_completion_date, :class => 'form-control datepicker' %>
    </div>

I did everything, but don't know why the datepicker gem is not working.. Is there any mistake i made there.. if any plz correct me and help..

Upvotes: 0

Views: 926

Answers (1)

Marcelo Austria
Marcelo Austria

Reputation: 951

This is turbolink issue, in your datepicker.js file change the

$(document).ready(function()

into

$(document).on('turbolinks:load', function ()

$(document).on('turbolinks:load', function () {
    // jquery javascript
    // your code snippet here
 $('.datepicker').datepicker({format: 'yyyy-mm-dd'});
}); 


document.addEventListener('turbolinks:load', function(event) {
    // vanilla javascript
    // your code snippet here
});

Upvotes: 1

Related Questions