Rails应用程序不会在生产中使用预编译的资产

时间:2022-03-02 23:19:54

I have a Rails App that loads all the assets on development just fine.

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

Whereas on the Production Server, it is still using the above code to load CSS. Shouldn't it be using the /assets/applicaiton.css file instead? I have run the rake assets:precompile task manually on production and I can see it has created the required files in the /public/assets folder.

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

So, what do I need to do to tell Rails to use the compressed files?

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

My production.rb looks like this :-

我的生产。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 个解决方案

#1


2  

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

您应该使用样式表_link_tag助手像这样调用css文件:

= 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

修改你的生产。将config.assets.compile设为true的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 = true

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

"Should work"

“应该”

#1


2  

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

您应该使用样式表_link_tag助手像这样调用css文件:

= 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

修改你的生产。将config.assets.compile设为true的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 = true

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

"Should work"

“应该”