我遵循Rails教程(https://www.railstutorial.org/book/basic_login)生成示例Rails应用。但是,下拉菜单仅在开发模式下有效。在生产模式下,单击“帐户”不会发生任何反应。我尝试以下操作,仍然无法解决。谁能帮我吗? 1)预编译资产 2)如有人提到的那样更改applicaiton.js中的引导程序和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>
以下是我的资产/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]
答案 0 :(得分:0)
您是否在开始生产实例之前尝试预编译资产?如果没有运行:
RAILS_ENV=production rake assets:clobber
RAILS_ENV=production rake assets:precompile
希望这会有所帮助。
答案 1 :(得分:0)
我的Rails应用存在类似问题。看到这里:Javascript in a fresh Rails 5.2.1 app on Heroku doesn't work properly
问题是我从
更改了uglifier
宝石
gem 'uglifier', '>= 1.3.0'
将几周前发布的4.1.18版本加载到
gem 'uglifier', '~> 3.0.4'
您应该更改
config.assets.debug = true
返回false,因为这将一个接一个地加载所有资产,而不是像在开发中那样在生产中最小化它们(在uglifier
的帮助下)。该错误仅来自uglifier
宝石以某种方式无法正常工作
编辑:
这些是我的application.js require语句:
//= 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]
这是我目前正在为Web开发课程使用的应用程序:
我也将继续学习本教程,现在在Rails教程的password_reset步骤中,并且现在可以使用此设置...
轨道5.2.1 Ruby 2.4.4p296 Windows 10教育版