如何将stylesheet_link_tag与bootstrap-sprockets一起使用?

时间:2015-04-30 18:03:01

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

我想从布局文件中的bootstrap-sass gem中加载bootstrap-sprockets(例如application.html.erb),而不是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中正确加载这些资源?

1 个答案:

答案 0 :(得分:0)

请检查您使用的文件扩展名可能是您使用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

祝你好运