简体   繁体   English

Rails 3.1资产管道在生产中

[英]Rails 3.1 assets pipeline in production

I am using the assets pipeline (in Rails 3.1.3) and am kind of struggling to make it work in production. 我正在使用资产管道(在Rails 3.1.3中)并且正在努力使其在生产中工作。

Situation 情况

In my /app/assets/stylesheets directory I have the following files: 在我的/app/assets/stylesheets目录中,我有以下文件:

application.css --> this is the default rails one
stylesheet.css --> This is my custom stylesheet

I spent a lot of time getting my stylesheet.css included in the /public/assets/ directory in production (by running rake assets:precompile ) and I finally made it by adding the following line into in my application.rb file: 我花了很多时间将我的stylesheet.css包含在生产中的/public/assets/目录中(通过运行rake assets:precompile ),最后通过在application.rb文件中添加以下行来实现:

    config.assets.precompile += ['stylesheet.css']

I know have the right precompiled stylesheet.css file in production. 我知道在生产中有正确的预编译stylesheet.css文件。

My Problem 我的问题

The problem I have is when using stylesheet_link_tag with my stylesheet.css file. 我遇到的问题是使用stylesheet_link_tagstylesheet.css文件。 It turns out: 事实证明:

<%= stylesheet_link_tag "stylesheet" %> is resolved into <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css"> in production I would expect the path to be resolved into /assets/stylesheet.css just like it does in development . <%= stylesheet_link_tag "stylesheet" %>被解析为<link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css"> 在制作中我希望路径到像在开发中一样解析成/assets/stylesheet.css

What is even more surprising is that application.css behaves perfectly even though <%= stylesheet_link_tag "application"%> resolves into <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css"> . 更令人惊讶的是,即使<%= stylesheet_link_tag "application"%>解析为<link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css">application.css表现完美。 <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css"> What I don't understand is that the public/stylesheets/ directory does not exist in rails 3.1. 我不明白的是rails 3.1中不存在public / stylesheets /目录。

Any idea ? 任何想法 ?

Richard Hulse answers pointed me to the right direction. 理查德·赫尔斯的回答向我指出了正确的方向。 What happens is really subtle.. 发生的事情真的很微妙......

The answer to my question is Rails 3.1 assets has no fingerprint in production . 我的问题的答案是Rails 3.1资产在生产中没有指纹

Basically, my project use mongoid instead of ActiveRecord. 基本上,我的项目使用mongoid而不是ActiveRecord。 According to Mongoid documentation about configuration, the application.rb file can be modified to not include ActiveRecord which means removing: 根据有关配置的Mongoid 文档 ,可以修改application.rb文件,使其不包含ActiveRecord ,这意味着删除:

require railties/all

And replacing it with: 并替换为:

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# require "sprockets/railtie" # Uncomment this line for Rails 3.1+

I was so used to doing this manipulation with rails 3.0.x that I did not pay attention to the comment related to Rails 3.1 我习惯于使用rails 3.0.x进行这种操作,我没注意与Rails 3.1相关的注释

My problem was that I was not requiring sprockets ! 我的问题是我不需要链轮!

Thank you all for your help ! 谢谢大家的帮助 !

The reason you can access stylesheet.css in development is because of how Sprockets works. 您可以在开发中访问stylesheet.css的原因是Sprockets的工作原理。

In development mode ALL requests to anything under /assets are sent to Sprockets to process. 在开发模式下,对/ assets下的任何内容的所有请求都将发送到Sprockets进行处理。 Sprockets will directly map requests to paths, one-to-one, so you can access any assets stored in app/assets/etc . Sprockets将直接将请求映射到路径,一对一,因此您可以访问存储在app/assets/etc任何资产。

All requests go through Sprockets; 所有请求都通过 Sprockets; it is serving the files to your browser. 它将文件提供给您的浏览器。

In production things are different. 在生产中,事情是不同的。 A fingerprint is added to filenames, and the expectation is that you'll precompile your assets to static files. 指纹被添加到文件名中,期望您将资产预编译为静态文件。 This is for performance reasons - Sprockets is not fast enough to serve lots of requests. 这是出于性能原因 - Sprockets不够快,无法满足大量请求。

Only those CSS and JS files referenced by the default manifests are compiled into application.css and application.js. 只有默认清单引用的CSS和JS文件才会被编译到application.css和application.js中。 Other files that you reference are not precompiled unless they are added to the config.assets.precompile array in your config file. 您引用的其他文件不会预编译,除非它们被添加到配置文件中的config.assets.precompile数组中。

You say that the files resolve to /stylesheets/stylesheet.css . 你说文件解析为/stylesheets/stylesheet.css The pipeline should generate a path like this in development: /assets/applicaton.css . 管道应该在开发中生成这样的路径: /assets/applicaton.css In production there should be a fingerprint in the filename. 在生产中,文件名中应该有指纹。 What you have posted suggested that the pipeline is not enabled (these are the old, pre 3.1, locations for the files). 您发布的内容表明管道未启用(这些是旧的,3.1之前的文件位置)。

If this is an upgraded app, it is likely that you have missed some crucial config option. 如果这是一个升级的应用程序,很可能你错过了一些关键的配置选项。 This is the main cause of dev->production issues. 这是开发生产问题的主要原因。 Check that the pipeline options are set exactly as they are in the last section of the pipeline guide. 检查管道选项的设置是否与管道指南的最后一部分 完全相同 (My guess is that you are missing config.assets.enabled = true in application.rb) (我的猜测是你在application.rb中缺少config.assets.enabled = true

And for clarity I would suggest changing the name of stylesheet.css to admin.css , while including this in the precompile array (as you already had done). 为清楚起见,我建议将stylesheet.css的名称更改为admin.css ,同时将其包含在预编译数组中(如您所做)。

With the config options set correctly, and your admin manifest included in precompile, you should have application.css available for the front end and admin.css available for the back-end, both linkable via the helper methods. 如果配置选项设置正确,并且您的管理清单包含在预编译中,则应该为前端提供application.css,为后端提供admin.css,这两种方法都可以通过帮助方法进行链接。

You application.css should be a manifest file, meaning that your when you run your program it should include your stylesheet.css automatically. application.css应该是一个清单文件,这意味着当您运行程序时,它应该自动包含stylesheet.css

make sure it has these two lines. 确保它有这两行。

application.css: application.css:

/*
 *= require_self
 *= require_tree . 
*/

If it does then something else isn't working properly, you shouldn't need the line: 如果它确实那么其他东西不能正常工作,你不应该需要这条线:

config.assets.precompile += ['stylesheet.css']

If that isn't working either make sure you have all the settings enabled from this Asset Pipeline guide. 如果这不起作用,请确保您已从此资产管道指南中启用了所有设置。

While I was looking around actionpack to see what might be causing this problem I found this little gem in the 3.1.1 changelog: 当我环顾actionpack以查看可能导致此问题的原因时,我在3.1.1日志中找到了这个小宝石:

javascript_path and stylesheet_path now refer to /assets if asset pipelining is on.

Soooo, If you're using 3.1.0 upgrade to 3.1.1 and see if it fixes it. Soooo,如果您正在使用3.1.0升级到3.1.1并查看它是否修复了它。 If you've already upgraded then were back to square one. 如果你已经升级了,那就回到原点了。 But it does seem to describe your problem since stylesheet_link_tag uses stylesheet_path interally. 但它确实描述了您的问题,因为stylesheet_link_tag使用stylesheet_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM