我正在通过Michael Hartl的Rails教程(RoR 5),我正在尝试创建一个下拉菜单作为Web应用程序的一部分。我已经仔细遵循了Hartl的说明,但登录后我无法在网页上显示该下拉列表。
Stack Overflow上讨论同样问题的过去主题是3年多了,因此大多数所提到的宝石(和相应的解决方案)已经过时,似乎并不适用于最新版本的Rails (本教程使用版本5.1.2)。
我尝试过以下修复(没有一个有效,有些导致错误):
使用"需要bootstrap-dropdown",在较新的bootstrap下载中可能已经过时
在coffee.js文件中包含$(document)。dropdown方法
我非常喜欢Rails(以及一般的编程),因此非常感谢您提供的任何建议。
以下是似乎相关/有用的文件:
的Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.2'
#bcrypt for password hashing
gem 'bcrypt', '3.1.11'
#Boostrap-Sass
gem 'bootstrap-sass', '3.3.7'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# gem 'therubyracer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
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', '1.3.13'
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
gem 'rails-controller-testing', '1.0.2'
gem 'minitest-reporters', '1.1.14'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
end
group :production do
gem 'pg', '0.18.4'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
应用/ Javascript角/ application.js中:
//= require rails-ujs
//= require bootstrap
//= require turbolinks
//= require_tree .
_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", root_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>
应用/视图/布局/ _rails_default.html.erb:
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
答案 0 :(得分:0)
当我做Michael的教程(2017年7月)时,我最终删除了gem文件中的所有版本。这样rails就可以加载每个版本的最新版本。 所以改变:
gem 'bootstrap-sass', '3.3.7'
只是:
gem 'bootstrap-sass'
等。对于gemfile中的每个gem 在我从不同的过时和不兼容的宝石做到这一点之前,我得到了各种奇怪的错误 祝你好运!
答案 1 :(得分:0)
我在处理 RoR 6 时遇到了同样的问题。经过一些调试后,我发现 _header 部分中的 logged_in? 辅助函数即使在登录。我终于在 if 块之前添加了帮助器 current_user。
<% current_user %>
<% if logged_in? %>
.
.
.
<% end %>
这似乎是一种解决方法,但为我解决了!