单击 bootstrap 下拉菜单时没有任何反应。怎么修?
Nothing happens when clicking bootstrap dropdown menu. How to fix?
这是我第一次使用 Bootstrap,在此先感谢您的帮助。我有一个带有下拉菜单的 header 元素。但是,当我单击下拉菜单时,没有任何反应。控制台中没有错误。以下是一些文件:
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Journal</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= render 'layouts/navigation' %>
<%= yield %>
</body>
</html>
_navigation.html.erb
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="row">
<!-- Elements visible all the time -->
<div class="col-sm-7">
<%= render 'layouts/navigation/header' %>
</div><!-- col-sm-7 -->
<!-- Elements collapsed on smaller devices -->
<div class="col-sm-5">
<%= render 'layouts/navigation/collapsible_elements' %>
</div><!-- col-sm-5 -->
</div><!-- row -->
</div><!-- container -->
</nav>
navigation_helper.rb
module NavigationHelper
def collapsible_links_partial_path
if user_signed_in?
'layouts/navigation/collapsible_elements/signed_in_links'
else
'layouts/navigation/collapsible_elements/non_signed_in_links'
end
end
end
_signed_in_links.html.erb
<li class="dropdown pc-menu">
<a id="user-settings" class="dropdown-toggle" data-toggle="dropdown" href="#">
<span id="user-name"><%= current_user.username %></span>
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><%= link_to 'Edit Profile', edit_user_registration_path %></li>
<li><%= link_to 'Log out', destroy_user_session_path, method: :delete %></li>
</ul>
</li>
<li class="mobile-menu">
<%= link_to 'Edit Profile', edit_user_registration_path %>
</li>
<li class="mobile-menu">
<%= link_to 'Log out', destroy_user_session_path, method: :delete %>
</li>
application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
宝石文件
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.1'
gem 'rails', '~> 6.0.3', '>= 6.0.3.5'
gem 'pg', '>= 0.18', '< 2.0'
gem 'devise'
gem 'puma', '~> 4.1'
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'
gem 'bootstrap_form'
gem 'uglifier'
gem 'jquery-rails'
gem 'bootsnap', '>= 1.4.2', require: false
gem 'bootstrap-sass', '~> 3.3.6'
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]
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'
gem 'listen', '~> 3.2'
# 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
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
javascript_pack_tag
will render a JavaScript file located in app/javascript/packs/
(Webpacker). In order to render a file in app/assets/javascripts/
(Sprockets), you'll need to use javascript_include_tag
.
或者,您可以通过 Webpacker 安装 Bootstrap。执行此操作的说明将根据您要使用的 Bootstrap 版本而有所不同,但这里是 a guide on installing Bootstrap 5 via Webpacker.
这是我第一次使用 Bootstrap,在此先感谢您的帮助。我有一个带有下拉菜单的 header 元素。但是,当我单击下拉菜单时,没有任何反应。控制台中没有错误。以下是一些文件:
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Journal</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= render 'layouts/navigation' %>
<%= yield %>
</body>
</html>
_navigation.html.erb
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="row">
<!-- Elements visible all the time -->
<div class="col-sm-7">
<%= render 'layouts/navigation/header' %>
</div><!-- col-sm-7 -->
<!-- Elements collapsed on smaller devices -->
<div class="col-sm-5">
<%= render 'layouts/navigation/collapsible_elements' %>
</div><!-- col-sm-5 -->
</div><!-- row -->
</div><!-- container -->
</nav>
navigation_helper.rb
module NavigationHelper
def collapsible_links_partial_path
if user_signed_in?
'layouts/navigation/collapsible_elements/signed_in_links'
else
'layouts/navigation/collapsible_elements/non_signed_in_links'
end
end
end
_signed_in_links.html.erb
<li class="dropdown pc-menu">
<a id="user-settings" class="dropdown-toggle" data-toggle="dropdown" href="#">
<span id="user-name"><%= current_user.username %></span>
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><%= link_to 'Edit Profile', edit_user_registration_path %></li>
<li><%= link_to 'Log out', destroy_user_session_path, method: :delete %></li>
</ul>
</li>
<li class="mobile-menu">
<%= link_to 'Edit Profile', edit_user_registration_path %>
</li>
<li class="mobile-menu">
<%= link_to 'Log out', destroy_user_session_path, method: :delete %>
</li>
application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
宝石文件
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.1'
gem 'rails', '~> 6.0.3', '>= 6.0.3.5'
gem 'pg', '>= 0.18', '< 2.0'
gem 'devise'
gem 'puma', '~> 4.1'
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'
gem 'bootstrap_form'
gem 'uglifier'
gem 'jquery-rails'
gem 'bootsnap', '>= 1.4.2', require: false
gem 'bootstrap-sass', '~> 3.3.6'
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]
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'
gem 'listen', '~> 3.2'
# 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
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
javascript_pack_tag
will render a JavaScript file located in app/javascript/packs/
(Webpacker). In order to render a file in app/assets/javascripts/
(Sprockets), you'll need to use javascript_include_tag
.
或者,您可以通过 Webpacker 安装 Bootstrap。执行此操作的说明将根据您要使用的 Bootstrap 版本而有所不同,但这里是 a guide on installing Bootstrap 5 via Webpacker.