生产环境下Rails应用未使用预编译资产。

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/application.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
1个回答

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

"

应该可以工作

"

我会尝试一下,谢谢。如果我不想让生产模式压缩文件,需要做什么呢?这样它们就像在开发模式下一样被提供了?这是一个后端应用程序,只有少数人会使用,所以真的不想被所有这些复杂性所困扰。 - Anuj Gakhar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接