繁体   English   中英

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

[英]Rails: How do you associate a stylesheet to a layout?

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

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

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

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

special.css isn't precompiled

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

默认情况下,Rails的只有预编译的application.cssapplication.js文件(或他们的.scss.less.coffee当量)。

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

config.assets.precompile += %w( special.css )

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

只是对接受的答案稍作修改 - 文档建议在config/initializers/assets.rb添加预编译资产。 这是有道理的,因此它们可以在所有环境中使用,而无需为每个环境重复配置。 所以我创建了该文件并添加了完全限定的config属性:

Rails.application.config.assets.precompile += %w( special.css )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM