【问题标题】:How do I use stylesheet_link_tag with bootstrap-sprockets?如何将 stylesheet_link_tag 与 bootstrap-sprockets 一起使用?
【发布时间】:2015-04-30 18:03:00
【问题描述】:

我想从我的布局文件(例如 application.html.erb )中的 bootstrap-sass gem 加载 bootstrap-sprockets,而不是在 application.scss 中。通常建议将bootstrapbootstrap-sprockets 放在application.scss 中,如下所示:

@import "bootstrap-sprockets";
@import "bootstrap";

然后像这样加载到您的application.scss

  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>

我想像这样加载它们:

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag    'bootstrap-sprockets', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag    'bootstrap', 'data-turbolinks-track' => true %>

这样我就可以根据某些应用程序变量在 erb 中加载它们。例如使用 bootswatch 和一个实例变量:

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag    'bootstrap-sprockets', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag    'bootswatch/'+ @account.style.name.downcase + '/variables',  'data-turbolinks-track' => true %>
<%= stylesheet_link_tag    'bootstrap', 'data-turbolinks-track' => true %>

尽管我在以这种方式加载这些资产时不断收到RoutingError,即使我在assets.rb 的预编译资产中拥有它,就像这样Rails.application.config.assets.precompile += %w( bootstrap-sprockets )

Started GET "/stylesheets/bootstrap-sprockets.css" for 127.0.0.1 at 2015-04-30 13:44:44 -0400

ActionController::RoutingError (No route matches [GET] "/stylesheets/bootstrap-sprockets.css"):
  actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.0) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.1) lib/rails/application.rb:164:in `call'
  rack (1.6.0) lib/rack/content_length.rb:15:in `call'
  thin (1.6.3) lib/thin/connection.rb:86:in `block in pre_process'
  thin (1.6.3) lib/thin/connection.rb:84:in `catch'
  thin (1.6.3) lib/thin/connection.rb:84:in `pre_process'
  thin (1.6.3) lib/thin/connection.rb:53:in `process'
  thin (1.6.3) lib/thin/connection.rb:39:in `receive_data'
  eventmachine (1.0.7) lib/eventmachine.rb:187:in `run_machine'
  eventmachine (1.0.7) lib/eventmachine.rb:187:in `run'
  thin (1.6.3) lib/thin/backends/base.rb:73:in `start'
  thin (1.6.3) lib/thin/server.rb:162:in `start'
  rack (1.6.0) lib/rack/handler/thin.rb:19:in `run'
  rack (1.6.0) lib/rack/server.rb:286:in `start'
  railties (4.2.1) lib/rails/commands/server.rb:80:in `start'
  railties (4.2.1) lib/rails/commands/commands_tasks.rb:80:in `block in server'
  railties (4.2.1) lib/rails/commands/commands_tasks.rb:75:in `tap'
  railties (4.2.1) lib/rails/commands/commands_tasks.rb:75:in `server'
  railties (4.2.1) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  railties (4.2.1) lib/rails/commands.rb:17:in `<top (required)>'
  bin/rails:8:in `require'
  bin/rails:8:in `<top (required)>'
  spring (1.3.4) lib/spring/client/rails.rb:28:in `load'
  spring (1.3.4) lib/spring/client/rails.rb:28:in `call'
  spring (1.3.4) lib/spring/client/command.rb:7:in `call'
  spring (1.3.4) lib/spring/client.rb:26:in `run'
  spring (1.3.4) bin/spring:48:in `<top (required)>'
  spring (1.3.4) lib/spring/binstub.rb:11:in `load'
  spring (1.3.4) lib/spring/binstub.rb:11:in `<top (required)>'
  /Users/cj/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  /Users/cj/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  bin/spring:13:in `<top (required)>'
  bin/rails:3:in `load'
  bin/rails:3:in `<top (required)>'
  -e:1:in `load'
  -e:1:in `<main>'

如何让这些资产从 gem 中正确加载?

【问题讨论】:

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


    【解决方案1】:

    请检查您使用的文件扩展名可能是您使用 yourFileName.css 但链接指向 yourFileName.scss 或 .sass

    还要检查你的 Gemfile 是否有

    gem 'bootstrap-sass'
    

    也可能此链接对您有用 https://github.com/twbs/bootstrap-sass/issues/692#issuecomment-55091209

    http://www.gotealeaf.com/blog/integrating-rails-and-bootstrap-part-1

    祝你好运

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 2023-03-25
      • 2021-08-22
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多