Rails:如何将样式表与布局相关联?

Mai*_*kel 5 css ruby-on-rails ruby-on-rails-3

我的应用程序有一个控制器,使用一个名为"特殊"的不同布局:

class SessionsController < ApplicationController
  layout "special"
  ...
end
Run Code Online (Sandbox Code Playgroud)

所以我创造了一个新的layouts/special.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <%= stylesheet_link_tag "special" %>
  <%= csrf_meta_tags %>
</head>
<body>
 <%= yield %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我还创建了一个新的对应的 stylesheets/special.css

问题是当我尝试使用"特殊"布局访问页面时,我得到一个例外:

Sessions中的Sprockets :: Helpers :: RailsHelper :: AssetPaths :: AssetNotPrecompiledError #new

special.css isn't precompiled
Run Code Online (Sandbox Code Playgroud)

我已经执行了bundle exec rake assets:precompile,但它没有解决问题.怎么了?如何将样式表与rails中的布局相关联?

Fáb*_*sta 3

默认情况下,Rails 仅预编译您的application.cssapplication.js文件(或它们的或.scss等效文件)。.less.coffee

如果您想要预编译其他文件,则必须将其添加到precompile的数组中config/environments/production.rb,如下所示:

config.assets.precompile += %w( special.css )
Run Code Online (Sandbox Code Playgroud)

请参阅:http ://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets