簡體   English   中英

Hartl 教程 Ch。 8 javascript 下拉菜單不會下拉,用戶無法登出

[英]Hartl tutorial Ch. 8 the dropdown menu via javascript will not drop down and user cannot log out

我一遍又一遍地檢查代碼,但似乎無法找到下拉菜單不起作用的原因。 因此,我無法查看下拉菜單中的鏈接是否正常工作。 此外,標題現在切斷了

. 任何幫助是極大的贊賞。 這是我的代碼。

_header.html.erb

//= require jquery
//= require bootstrap
//= require jquery_ujs
//= require turbolinks
//= require_tree .

會話控制器

source 'https://rubygems.org'

gem 'test-unit'

gem 'rails-perftest'

gem 'minitest'

gem 'bcrypt'

gem 'coffee-script-source', '1.8.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use sqlite3 as the database for Active Record
gem 'bootstrap-sass'

gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# 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'
  gem 'sqlite3'
  # Use SCSS for stylesheets
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

group :test do 
  gem 'minitest-reporters'
  gem 'mini_backtrace'
  gem 'guard-minitest'


group :production do
  gem 'pg'
  gem 'rails_12factor'
end

end

會話助手

module SessionsHelper def log_in(user) session[:user_id] = user.id end def current_user @current_user ||= User.find_by(id: session[:user_id]) end def logged_in? !current_user.nil? end end

應用程序.js

 //= require jquery //= require bootstrap //= require jquery_ujs //= require turbolinks //= require_tree .

文件

source 'https://rubygems.org' gem 'test-unit' gem 'rails-perftest' gem 'minitest' gem 'bcrypt' gem 'coffee-script-source', '1.8.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.0' # Use sqlite3 as the database for Active Record gem 'bootstrap-sass' gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.1.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' # 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' gem 'sqlite3' # Use SCSS for stylesheets # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' gem 'spring' end group :test do gem 'minitest-reporters' gem 'mini_backtrace' gem 'guard-minitest' group :production do gem 'pg' gem 'rails_12factor' end end

您是否在 custom.css.scss 文件的頂部添加了這兩行?

@import "bootstrap-sprockets"; @import "引導程序";

您的 _header.html.erb 在我的應用中運行良好。 我在這里看到的唯一區別是您使用: gem 'bootstrap-sass' 和我使用: gem 'bootstrap-sass', '3.2.0.0'

當用戶未登錄時,不會顯示下拉菜單。

您沒有發布您的 ApplicationController 文件。 你加了嗎

include SessionsHelper

在應用程序控制器中? 我認為登錄方法需要它。

去登錄一個任何用戶,然后查看源頁面,看看你是否已經翻倍了及其所有以下內容。 它們在 application.html.erb 文件中重復。 如果您轉到 application.html.erb 並注釋掉標題及其所有內容,然后刷新頁面並再次檢查,下拉鏈接應該會顯示出來。 這里的問題是,在教程中它沒有提到任何內容,但是按照本書中的步驟,您在前一章中創建了這個標題。 因此,應用程序文件中的標頭具有優先權,它會阻止 _header.html.erb 文件內容。

下拉菜單未顯示,因為引導程序需要 jquery 作為依賴項(在 application.js 中需要的順序很重要)。 require jquery_ujs之后放置require bootstrap應該可以解決問題。

我遇到了這個問題,跟着書到“t”,到了第 8 章,在陽光下嘗試了一切。 我決定離開並玩一些 Destiny2 並且在殺死新手的過程中它來找我。 *.js(JavaScript 文件)中的“//”是單行注釋。 查看有關它的官方 W3School 信息JavaScript 評論

因此,您需要做的就是從 application.js 文件中的所有行條目中刪除“//”。

應該看起來像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM