我的应用程序有一个 Controller ,它使用称为“特殊”的不同布局:
class SessionsController < ApplicationController
layout "special"
...
end
所以我创建了一个新的layouts/special.html.erb
:
<!DOCTYPE html>
<html>
<head>
<title></title>
<%= stylesheet_link_tag "special" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
我还新建了一个对应的stylesheets/special.css
问题是当我尝试访问具有“特殊”布局的页面时出现异常:
Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Sessions#new
special.css isn't precompiled
我已经执行了 bundle exec rake assets:precompile
,但它没有解决问题。
怎么了?如何将样式表关联到 Rails 中的布局?
最佳答案
默认情况下,Rails 只预编译您的 application.css
和 application.js
文件(或它们的 .scss
、.less
或 .coffee
等价物)。
如果要预编译其他文件,必须将其添加到 config/environments/production.rb
的 precompile
数组中,如下所示:
config.assets.precompile += %w( special.css )
参见:http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
关于css - rails : How do you associate a stylesheet to a layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9781281/