【问题标题】:adding "//= require bootstrap" leads to errors in nav-pills links添加“//= 需要引导程序”会导致 nav-pills 链接出错
【发布时间】:2016-05-04 19:47:13
【问题描述】:

我的 rails 应用程序有一个完美的导航药丸菜单:

<div class="col-lg-2 col-sm-3">
   <ul class="nav nav-pills nav-stacked">
      <li class="<%= 'active' if current_page?(articles_path) %>"><%= link_to "articles", articles_path, data: { toggle: "pill"} %></li>
      <li class="<%= 'active' if current_page?(appointments_path) %>"><%= link_to "appointments", appointments_path, data: { toggle: "pill"} %></li>
      <li class="<%= 'active' if current_page?(users_path) %>"><%= link_to "users", users_path, data: { toggle: "pill" } %></li>
   </ul>
</div>

然后我在其他地方添加了一个下拉菜单。因此我需要在 application.js 中添加“//= require bootstrap”:

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

下拉菜单运行良好,但由于我添加了“需要引导程序”,导航药丸菜单中的链接不再起作用。当我单击药丸时,什么也没有发生,并且 Firefox 在控制台中显示例如此错误:

错误:语法错误,无法识别的表达式:/appointments

如果我删除“需要引导程序”,导航药丸菜单中的链接会再次起作用,但下拉菜单不会下拉。

当我在 application.js 中使用“需要引导程序”时,我必须在 nav-pills 菜单中更改哪些建议?

更多信息

宝石文件:

source 'https://rubygems.org'

ruby '2.2.4'

gem 'rails',        '4.2.6'
gem 'bcrypt',       '3.1.11'
gem 'bootstrap-sass',    '3.3.6' # needed for bootstrap css
gem 'sass-rails',   '5.0.4'
gem 'uglifier',     '3.0.0'
gem 'coffee-rails', '4.1.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks',   '2.5.3'
gem 'jbuilder',     '2.4.1'
gem 'sdoc',         '0.4.0', group: :doc

group :development, :test do
  gem 'sqlite3',     '1.3.11'
  gem 'byebug',      '8.2.2'
  gem 'web-console', '3.1.1'
  gem 'spring',      '1.6.4'
end

group :test do
  gem 'minitest-reporters', '1.1.8'
  gem 'mini_backtrace',     '0.1.3'
  gem 'guard', '2.13.0'
  gem 'guard-minitest',     '2.4.4'
end

group :production do
  gem 'pg',             '0.18.4'
  gem 'rails_12factor', '0.0.3'
  gem 'puma',           '3.2.0'
end

application.scss:

@import "bootstrap-sprockets";
@import "bootstrap";
@import "/*";
@import "/**/*";

routes.rb:

Rails.application.routes.draw do

  get 'sessions/new'

  root             'static_pages#home'
  get 'about'   => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'articles' => 'administration#articles'
  get 'appointments' => 'administration#appointments'
  get 'createarticle' => 'articles#new'
  get 'createappointment' => 'appointments#new'
  get 'interface' => 'administration#interface'
  get 'help' => 'administration#help'
  get 'createuser' => 'users#new'
  get 'users' => 'administration#users'
  get    'adminlogin'   => 'sessions#new'
  post   'adminlogin'   => 'sessions#create'
  delete 'adminlogout'  => 'sessions#destroy'

  resources :users
  resources :articles
  resources :appointments
end

完整的错误堆栈跟踪:

Sizzle</Sizzle.error() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:1503
Sizzle</Sizzle.tokenize() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:2160
Sizzle</Sizzle.select() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:2581
Sizzle() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:904
.find() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:2827
jQuery.fn.init() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:2950
jQuery() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:76
Tab.prototype.show() bootstrap.self-d0e973a52d36a01036265aeefaf2d1ae3499d2cb231a9be3bd846432daf231de.js:2097
Plugin/<() bootstrap.self-d0e973a52d36a01036265aeefaf2d1ae3499d2cb231a9be3bd846432daf231de.js:2170
.each() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:371
jQuery.prototype.each() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:138
Plugin() bootstrap.self-d0e973a52d36a01036265aeefaf2d1ae3499d2cb231a9be3bd846432daf231de.js:2165
clickHandler() bootstrap.self-d0e973a52d36a01036265aeefaf2d1ae3499d2cb231a9be3bd846432daf231de.js:2194
jQuery.event.dispatch() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:5226
jQuery.event.add/elemData.handle() jquery.self-660adc51e0224b731d29f575a6f1ec167ba08ad06ed5deca4f1e8654c135bf4c.js:487

【问题讨论】:

  • 您能否添加更多信息,例如您使用的宝石等
  • 从错误信息中,我猜你的 current_page 出错了?方法。你也能给我吗?顺便说一句,希望您在 application.scss 中添加了 sprockets 和 bootstrap 的包含
  • Nyawawa... 请显示您的 routes.rb 文件。还有你的完整错误堆栈跟踪
  • 添加了我的 routes.rb 文件
  • 添加了堆栈跟踪@Ritikesh:current_page?方法来自 rails 本身 (api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html)。我还没有为 sprockets 和 bootstrap 添加包含,因为当我添加“*= require bootstrap”时它说它找不到文件

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


【解决方案1】:

我为 css 和 js 添加了这些东西,之后所有工作对我来说都很好。

    %script{src: "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js",
        integrity: "sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS",
        crossorigin: "anonymous"}

你也在使用 -> css: @import 'bootstrap';

【讨论】:

  • 我在哪里可以添加这个?我没有在 css 文件中添加 @import 'bootstrap' 或任何类似的东西
  • 我强烈建议您不要依赖第三方 CDN 来为您的资产提供实时产品应用程序。而且由于他使用的是gem,他还不如在本地编译资产。
  • 那 CDN 呢。有一次我在 Heroku 上的引导程序有问题... Mby 我做错了,但是当我将 CDN 添加到 layouts/application.html.haml 时,这对我有用...
  • app/assets/stylesheets/custom.css.scss @import "bootstrap";从 -> railstutorial book 获取 - 我认为这是适合您情况的解决方案。
猜你喜欢
  • 2014-05-31
  • 2016-02-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多