汉堡切换不工作轨道5

时间:2017-06-25 15:29:29

标签: html ruby-on-rails twitter-bootstrap

我正在制作一个可切换的菜单但它不起作用,也就是说,单击右侧的图标时不会显示。我的工作环境是Rails v5.1.1 + Ruby v2.3.3 + bootstrap 4.0.0.alpha6。

的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.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# 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'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# 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'

#-------------------------------------------------
# Gemas especificas para el proyecto
gem 'amcharts.rb', '~> 3.11', '>= 3.11.2.17'
gem 'devise', '~> 4.2', '>= 4.2.1'
gem 'mongo', '~> 2.2', '>= 2.2.1'
gem 'pg', '~> 0.21.0'
gem 'font-awesome-rails'
gem 'bootstrap', '~> 4.0.0.alpha6'
#-------------------------------------------------

# 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]
  # 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'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

_header文件

<!-- Header -->

<nav class="navbar navbar-fixed-top navbar-inverse bg-primary">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="navbar-collapse collapse" id="collapsingNavbar">
    <ul class="navbar-nav">
      <li class="active"><%= link_to 'Inicio', root_path %></li>
      <li><%= link_to 'Nosotros', root_path %></li>
      <li><%= link_to 'Servicios', root_path %></li>
      <li><%= link_to 'Contacto', root_path %></li>
    </ul>
  </div>
</nav>

<!-- Fin Header -->

的application.js

//= require rails-ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .

application.scss文件

@import "bootstrap";

从bootstrap 4页获取的示例

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

提出错误行#10 enter image description here

1 个答案:

答案 0 :(得分:0)

这里是你的_header文件的一些更正,其中包含bootstrap 4作为你的通知,bootstrap 4和3有一些区别,这里有你的参考链接bootstrap 4 components navbar

<nav class="navbar  navbar-toggleable-md navbar-fixed-top navbar-inverse bg-primary">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="navbar-collapse collapse" id="collapsingNavbar">
    <ul class="navbar-nav">
      <li class="nav-item active"><%= link_to 'Inicio', root_path, class: 'nav-link' %></li>
      <li class="nav-item"><%= link_to 'Nosotros', root_path, class: 'nav-link'  %></li>
      <li class="nav-item"><%= link_to 'Servicios', root_path, class: 'nav-link'  %></li>
      <li class="nav-item"><%= link_to 'Contacto', root_path, class: 'nav-link'  %></li>
    </ul>
  </div>
</nav>

请检查您的app / views / layouts / application.html.erb 确保在head标签内部有视口元标记以检查屏幕尺寸

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
...
</head>

看来你也缺少jquery(请记住使用rails-ujs的默认rails 5.1但是bootstrap需要jquery),下面添加jquery rails

# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-turbolinks'

内部资产/ javascripts / applications.js(在bootstrap之前将命令放在下面)

//= require jquery
//= require jquery.turbolinks