3

我有一个 Rails 应用程序,可以很好地加载开发中的所有资产。

<link rel="stylesheet" href="/assets/reset.css" type="text/css" media="screen" title="no title" />
<link rel="stylesheet" href="/assets/text.css" type="text/css" media="screen" title="no title" />
<link rel="stylesheet" href="/assets/buttons.css" type="text/css" media="screen" title="no title" />
<link rel="stylesheet" href="/assets/theme-default.css" type="text/css" media="screen" title="no title" />
<link rel="stylesheet" href="/assets/login.css" type="text/css" media="screen" title="no title" />
<link rel="stylesheet" href="/assets/notify.css" type="text/css" media="screen" title="no title" />

而在生产服务器上,它仍然使用上面的代码来加载 CSS。它不应该使用 /assets/applicaiton.css 文件吗?我已经在生产环境中手动运行了 rake assets:precompile 任务,我可以看到它已经在 /public/assets 文件夹中创建了所需的文件。

那么,我需要做什么来告诉 Rails 使用压缩文件呢?

我的 production.rb 看起来像这样:-

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = true

 # Compress JavaScripts and CSS
 config.assets.compress = true

 # Don't fallback to assets pipeline if a precompiled asset is missed
 config.assets.compile = false

 # Generate digests for assets URLs
 config.assets.digest = true
4

1 回答 1

2

You should use the stylesheet_link_tag helper to call the css file like this :

= stylesheet_link_tag "application", :media => "all"

and insert your stylesheets inside the application stylesheet file like :

= require reset
= require text
= require button
...

Modify then your production.rb file to set the config.assets.compile as true

 # Disable Rails's static asset server (Apache or nginx will already do this)
 config.serve_static_assets = true

 # Compress JavaScripts and CSS
 config.assets.compress = true

 # Don't fallback to assets pipeline if a precompiled asset is missed
 config.assets.compile = true

 # Generate digests for assets URLs
 config.assets.digest = true

"Should work"

于 2012-11-22T15:35:58.933 回答