引导 datepicker rails gem 不工作
Posted
技术标签:
【中文标题】引导 datepicker rails gem 不工作【英文标题】:bootstrap datepicker rails gem is not working 【发布时间】:2018-02-18 22:42:34 【问题描述】:我不明白为什么 datepicker gem 不起作用。这是我的应用程序的代码:
这是我的 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'
这是我的 application.css 文件:
/*
* 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
*/
这是我的 application.js 文件:
// 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 .
这是我的 datepicker.js 文件:
$(document).ready(function()
$(function()
$('.datepicker').datepicker(format: 'yyyy-mm-dd');
);
);
这是我的 _form.html.erb 文件的 sn-p:
</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>
我做了所有事情,但不知道为什么 datepicker gem 不起作用..我在那里犯了任何错误..如果有任何错误请纠正我并提供帮助..
【问题讨论】:
我怀疑这是一个 turbolinks 问题。呼叫$(document).ready....
可能无法正常工作。解决方案取决于您的 rails 版本,但请查看答案 here 和 here。
非常感谢您的帮助...@TomLord
【参考方案1】:
这是 turbolink 问题,在你的 datepicker.js 文件中更改
$(document).ready(function()
进入
$(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
);
【讨论】:
以上是关于引导 datepicker rails gem 不工作的主要内容,如果未能解决你的问题,请参考以下文章