Rails App不在生产中使用预编译资产

时间:2012-11-22 15:27:09

标签: ruby-on-rails

我有一个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" />

然而在Production Server上,它仍然使用上面的代码来加载CSS。它不应该使用/assets/applicaiton.css文件吗?我已经运行了rake资产:在生产时手动预编译任务,我可以看到它在/ 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

1 个答案:

答案 0 :(得分:2)

您应该使用stylesheet_link_tag帮助程序来调用css文件,如下所示:

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

并将样式表插入应用程序样式表文件中,如:

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

然后修改您的production.rb文件,将config.assets.compile设置为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

“应该工作”