Rails Bootstrap、jQuery 和 jQuery-UI 在 Heroku 上不工作

Rails with Bootstrap, jQuery, and jQuery-UI not working on Heroku

我在 Rails 5 上有一个应用程序,它使用 Bootstrap、jQuery 和 jquery_ui。我使用 jquery_ui 作为下拉菜单。

下拉菜单在开发和生产中都损坏了,我不知道如何解决这个问题。

A screenshot of the errors/warnings in the web console (production) -- 显然我不能嵌入图像,所以我不得不凑合使用 link.

宝石文件:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0'

# Use sqlite3 as the database for Active Record
gem 'sqlite3', group: :development


#### HEROKU SPECIFIC GEMS #####
gem 'pg', '0.18.1', group: :production
################################

# Use Puma as the app server
gem 'puma', '~> 3.0'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'

# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails', '~> 5.0', '>= 5.0.5'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'

# Authentication with Devise
gem 'devise'

# Bootstrap framework
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.6'

# Markdown parser
gem 'redcarpet', '~> 3.3', '>= 3.3.4'

# Markdown syntax highlighting
gem 'coderay'

# Admin panel
gem 'remotipart', github: 'mshibuya/remotipart'
gem 'rails_admin', '>= 1.0.0.rc'

# Forms the easy way
gem 'simple_form', '~> 3.2', '>= 3.2.1'

gem 'seed_dump'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

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

ruby '2.3.1'

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, vendor/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 jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require_tree .

Application.css.scss

/*
 * 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, vendor/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.
 *
 */
 @import 'jquery-ui/core.css';
 @import 'jquery-ui/datepicker.css';
 @import 'jquery-ui/theme.css';
 @import "bootstrap-sprockets";
 @import "bootstrap";

 .basic {
  td {
    padding: 5px;
  }
  border-collapse: collapse;
}

.alert-alert {
  @extend .alert-warning;
}

.alert-notice {
  @extend .alert-info;
}

#accordion  {
  h3:focus {
    outline: none;
  }
  .ui-state-active {
    background-color: #00AAE3;
  }
}

...我哪里错了?我过去三天一直在研究这个,但没有骰子。

编辑:

运行 一些测试。如果我在 Application.js 中将 Bootstrap 放在 jQuery 之上,下拉菜单会在开发中起作用,但不会在生产中起作用(像这样):

//= require bootstrap-sprockets
//= require jquery
//= require jquery_ujs
//= require_tree .

但是,我在开发时遇到引用错误 ("can't find variable jQuery")(在生产时没有错误)。

如果我按照编辑上方 Application.js 中的顺序要求它们,我不会收到任何参考错误,但下拉菜单在开发和生产中都被破坏了。

无论如何,我无法让他们投入生产。

您需要将 bootstrap-sprockets 移到 jquery_ujs 之后。您的文件应如下所示:

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require_tree .

在文档中 here 它提到了这一点。

此外,在将 sass 与 jquery-ui-rails 一起使用时,您似乎必须请求 uire 模块。在 this 线程中找到答案。将您的 application.css.scss 更改为:

 @import 'jquery-ui/core.css';
 @import 'jquery-ui/datepicker.css';
 @import 'jquery-ui/theme.css';
 @import "bootstrap-sprockets";
 @import "bootstrap";

我终于弄明白了这个问题,让下拉菜单在开发和生产中都起作用。 The problem was that I was requiring Bootstrap twice. 事实上,我要求所有的东西都两次。所以我决定看看我的 application.html.erb,你瞧,这就是我发现的:

<%= csrf_meta_tags %>

  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag 'application' %>
  <%= javascript_include_tag :application %>
</head>

不,不好。一切都被包括了两次。所以我去掉了第一个javascript_include_tag,我所有的问题都解决了!

决赛 application.js:

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require_tree .

决赛 application.css.scss:

@import 'jquery-ui/core.css';
@import 'jquery-ui/datepicker.css';
@import 'jquery-ui/theme.css';
@import "bootstrap-sprockets";
@import "bootstrap";

.basic {
 td {
    padding: 5px;
  }
  border-collapse: collapse;
}

.alert-alert {
  @extend .alert-warning;
}

.alert-notice {
  @extend .alert-info;
}

#accordion  {
  h3:focus {
    outline: none;
  }
  .ui-state-active {
    background-color: #00AAE3;
  }
}

最终 application.html.erb(我所有问题的根源):

  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag :application %>
</head>

是的,愚蠢的错误,很容易被忽视。