首页 > 解决方案 > Bootstrap 3下拉菜单在生产模式rails 5.2中不起作用

问题描述

我按照 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 .

这是我的宝石文件:

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]

标签: ruby-on-rails

解决方案


我的 Rails 应用程序也有类似的问题。请参阅此处:Heroku 上全新 Rails 5.2.1 应用程序中的 Javascript 无法正常工作

问题是uglifier我改变它的宝石

gem 'uglifier', '>= 1.3.0'

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

gem 'uglifier', '~> 3.0.4'

你应该改变

config.assets.debug = true

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

编辑:

这些是我的 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 论坛

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

Rails 5.2.1 Ruby 2.4.4p296 Windows 10 教育版


推荐阅读