【问题标题】:Rails: How do you associate a stylesheet to a layout?Rails:如何将样式表关联到布局?
【发布时间】:2012-03-20 04:17:49
【问题描述】:

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

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 在会话中#new

special.css isn't precompiled

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

【问题讨论】:

    标签: css ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    默认情况下,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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-11
        • 2012-08-27
        • 1970-01-01
        • 1970-01-01
        • 2013-07-05
        • 1970-01-01
        • 2011-03-06
        • 1970-01-01
        相关资源
        最近更新 更多