【问题标题】:Bootstrap 3 dropdown menu not working in production mode rails 5.2Bootstrap 3下拉菜单在生产模式rails 5.2中不起作用
【发布时间】:2018-09-04 04:36:20
【问题描述】:

我按照 rails 教程 (https://www.railstutorial.org/book/basic_login) 生成示例 rails 应用程序。但是,下拉菜单仅适用于开发模式。在生产模式下,单击“帐户”时没有任何反应。我尝试了以下事情,仍然无法解决。谁能帮我吗? 1) 预编译资产 2)正如有人提到的那样,在 applicaiton.js 中更改 bootstrap 和 jquery 顺序

这里是_header.html.erb文件代码:

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home", home_path %></li>
        <li><%= link_to "Help", help_path %></li>
        <% if logged_in? %>
          <li><%= link_to "Users", '#' %></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              Account <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><%= link_to "Profile", current_user %></li>
              <li><%= link_to "Settings", '#' %></li>
              <li class="divider"></li>
              <li>
                <%= link_to "Log out", logout_path, method: :delete %>
              </li>
            </ul>
          </li>
        <% else %>
          <li><%=link_to "Log in", login_path %></li>
        <% end %>
      </ul>
    </nav>
  </div>
</header>

下面是我的 assets/javascripts/application.js 文件:

//= require jquery

//= require bootstrap

//= require activestorage

//= require turbolinks

//= require_tree .

这是我的 gem 文件:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

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

# bootstrap
# gem 'bootstrap-sass'
gem 'bootstrap-sass', '~> 3.3.7'
# gem 'bootstrap', '~> 4.1.3'

# jquery
gem 'jquery-rails', '~>4.3'

# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# 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 'duktape'
# 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'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

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

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3'
end

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

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'

  gem 'minitest'
  gem 'minitest-reporters'

  gem 'rails-controller-testing'
end

group :production do
  gem 'pg'
end

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

【问题讨论】:

  • 如果我设置 config.assets.debug = true,它将起作用。但仍然无法弄清楚为什么会这样。

标签: ruby-on-rails


【解决方案1】:

您是否尝试在启动生产实例之前预编译资产?如果不运行:

RAILS_ENV=production rake assets:clobber
RAILS_ENV=production rake assets:precompile

希望这会有所帮助。

【讨论】:

  • 感谢您的帮助!刚试过,不行:(
  • 然后将 duktape gem 移至开发组?抱歉,它对您不起作用,但这是我的解决方案....尝试其他版本的 uglifier,至少对于 Heroku 和 ym wndows 机器,它解决了我的问题
【解决方案2】:

我的 Rails 应用也有类似的问题。见这里:Javascript in a fresh Rails 5.2.1 app on Heroku doesn't work properly

问题是我从 uglifier 更改它的 gem

gem 'uglifier', '>= 1.3.0'

其中加载了几周前发布的4.1.18版本

gem 'uglifier', '~> 3.0.4'

你应该改变

config.assets.debug = true

返回 false 因为这会在生产中一一加载所有资产,而不是像在开发中那样在生产中缩小它们(在uglifier 的帮助下)。错误只是来自uglifier gem 以某种方式无法正常工作

编辑:

这些是我的 application.js 要求语句:

//= require jquery
//= require rails-ujs
//= require activestorage
//= require bootstrap
//= require turbolinks
//= require_tree .

这是我的 GemFile:

# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.4.4'

gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'rails', '~> 5.2.1'
gem 'sass-rails', '~> 5.0'
gem 'turbolinks', '~> 5'
gem 'uglifier', '~> 3.0.4'

gem 'bootsnap', '>= 1.1.0', require: false

gem 'bcrypt', '~> 3.1.7'
gem 'bootstrap-sass'
gem 'bootstrap-will_paginate'
gem 'faker'
gem 'friendly_id', '~> 5.2.0'
gem 'jquery-rails'
gem 'rails-i18n'
gem 'will_paginate'

group :development, :test do
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'duktape'
end

group :test do
  gem 'capybara', '>= 2.15', '< 4.0'
  gem 'chromedriver-helper'
  gem 'rails-controller-testing'
  gem 'selenium-webdriver'
end

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

这是我目前正在为我的网络开发课程工作的应用程序:

Ruby on Rails Forum

我也将学习本教程,并在 Rails 教程的 password_reset 步骤中,它现在正在使用此设置...

导轨 5.2.1 红宝石 2.4.4p296 Windows 10 教育版

【讨论】:

  • 感谢您的帮助!我试过了,但还是不行。我使用 Chorme inspect 进行检查,发现它显示“Uncaught TypeError: ht.each is not a function”。
  • 这是我遇到的确切错误。 uglifier 就是解决这个问题的方法。如果您在生产中使用本地机器 testibg,我希望您做到了 bundle updaterake assets:precompile。如果不是我的最后一个解决方案是在你的production.rb 中用js_compressor: uglifier 注释掉这一行,因为你只使用外部JS,而这些在生产中失败了,因为uglifier gem 无法正确完成它的工作
  • 请检查我提供的链接,问题末尾还有另一个链接到我的 BitBucket 存储库。还要看看这个:stackoverflow.com/questions/52138549/… 所以你知道我有确切的ht.each is not a function 问题,因为uglifier gem
  • 确保使用 ~&gt; 3.0.4 而不是 &gt;=3.0.4 作为你的 uglifier gem 版本。它必须是 uglifier,因为当您的 JS 文件没有像在生产中那样压缩成一个大 JS 文件时,它们在开发中可以正常工作
  • 是的,我确实更新了 uglifier ~> 3.0.4 并捆绑了更新/安装、预编译,它仍然没有解决我的问题。这让我发疯。顺便说一句,我正在使用 bootstrap 3、rails 5.2.1 和 ruby​​ 2.5.1(实际上我尝试了 ruby​​ 2.3.3,仍然有这个问题)。这种情况很可能是js压缩器造成的,但还是不知道根本原因在哪里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2015-11-25
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多