简体   繁体   中英

Foundation 5 top-bar in Rails app not working

After trying different examples and running over my code many, many times, I cannot figure out why the drop-down menu on my top-bar isn't functional. I've tried the 'no-data-turbolinks' trick which came up in every search, to no avail.

I've also tried adding jquery.turbolinks. It's getting pretty frustrating.

I've even loaded the html for Zurb's own top-bar example. No worky-worky.

I'm using Rails 4.2.0, jquery-rails and foundation-rails 5.5.0.

I'm not sure what code to include, so if you have any questions, I'll be happy to incorporate it if you need it.


The following is my _navbar.html.erb (which is ac/p from the Foundation website's top-bar example):

<nav class="top-bar" data-topbar role="navigation">
  <ul class="title-area">
    <li class="name">
      <h1><a href="#">My Site</a></h1>
    </li>
     <!-- Remove the class "menu-icon" to get rid of menu icon. Take out "Menu" to just have icon alone -->
    <li class="toggle-topbar menu-icon"><a href="#"><span>Menu</span></a></li>
  </ul>

  <section class="top-bar-section">
    <!-- Right Nav Section -->
    <ul class="right">
      <li class="active"><a href="#">Right Button Active</a></li>
      <li class="has-dropdown">
        <a href="#">Right Button Dropdown</a>
        <ul class="dropdown">
          <li><a href="#">First link in dropdown</a></li>
          <li class="active"><a href="#">Active link in dropdown</a></li>
        </ul>
      </li>
    </ul>

    <!-- Left Nav Section -->
    <ul class="left">
      <li><a href="#">Left Nav Button</a></li>
    </ul>
  </section>
</nav>

This is my application.html.erb:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%= content_for?(:title) ? yield(:title) : "foundation-rails" %></title>


    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag "vendor/modernizr" %>
    <%= javascript_include_tag "application" 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
  </head>

  <%= render partial: "elements/navbar" %>

  <body>

    <%= yield %>

  </body>
  <%= render partial: "elements/footer" %>
</html>

And my application.js file (after removing the reference for turbolinks):

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require foundation
//= require_tree .

$(function(){ $(document).foundation(); });

My Gemfile (after removal of turbolinks):

source 'https://rubygems.org'

gem 'rails', '>= 4.2.0'


group :test, :development do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
  gem 'sqlite3', require: false
  gem 'database_cleaner'
  gem 'capybara'
end

group :production do
  gem 'thin', require: false
  gem 'pg', require: false
end

gem 'bundler'
gem 'foundation-rails', '>= 5.5'
gem 'sass-rails'
gem 'sass', '>= 3.3'
gem 'nav_lynx'
gem 'jbuilder', '~> 2.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'


gem 'uglifier', '>= 1.3.0'

gem 'sdoc', '~> 0.4.0',          group: :doc, require: false


gem 'spring',        group: :development

尝试在结束body标记之前移动它:

<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>

After running rails g foundation:install and allowing the command to overwrite the main application ERB file, you'll end up with a turbolinks code in your javascript_include_tag.

In your application.html.erb, change this...

<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>

to this...

<%= javascript_include_tag "application" %>

This single line code change should fix it. It worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM