2012-11-22 57 views
3

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

而在生产服务器上,它仍然使用上面的代码来加载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 

回答

2

您应该使用stylesheet_link_tag助手调用CSS文件是这样的:

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

,插入你的样式表应用样式表文件中如:

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

修改然后您的production.rb文件设置co nfig.assets.compile为真

# 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 

“应该工作”

+0

我会尽力的,谢谢。如果我不希望生产模式压缩文件,我需要做什么?所以他们会像他们处于开发模式一样服务?这是一个后端应用程序,只有少数人会使用,所以真的不想被这些复杂问题困扰。 –

相关问题