Ruby On Rails 3.1-资产管道-资产呈现两次 [英] Ruby On Rails 3.1 - assets pipeline - assets rendered twice

查看:56
本文介绍了Ruby On Rails 3.1-资产管道-资产呈现两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大更新:

当我终于找到真正的解决方案时,我也发现了真正的问题.当我在这里写下很多无用的信息时,考虑到实际的问题,我在对问题进行大量更新,以便其他人可以轻松地找到正在发生的事情并可以看到解决方案.

As I finally found the real solution, I also discovered the real problem. As I wrote down here a lot of useless information, considering the real problem, I'm making a huge update of the question so that other people can find easily what's going on and can see the solution.

问题: 这是由于Rails 3.1的资产管道造成的.

The Problem: It's because of the assets pipeline of Rails 3.1

实际上……这很简单:资产在开发环境中被渲染了两次.经过大量调查,我发现我的Rails 3.1服务器正在从"app/assets"和"public/assets"文件夹中呈现资产.因此,我复制了每个.js和.css文件,这破坏了我所有的javascript动画(是的……通常将两次相同的事件和处理程序绑定到同一元素不是您想要的……).

Actually... It's an easy one: the assets were rendered twice in development-environment. Doing lot of investigations shew me that my Rails 3.1 server was rendering the assets from both the "app/assets" and "public/assets" folders. So, I had every .js and .css files duplicated, which was breaking all my javascript animations (yeah... binding twice the same event and handler to the same element is not what you want... normally).

如果问题突然出现,那是因为我必须运行"rake asset:precompile"来部署我的应用程序.从那以后,当我的应用程序在开发中运行时,服务器将呈现静态的预编译资产和动态的预编译资产.

And if the problem appeared all of a sudden, that was because I had to run "rake assets:precompile" to deploy my application. Since that, when my application was running in development, the server was rendering the static precompiled assets and the dynamic precompiled assets.

解决方案(现在下面有几行更好)-但您仍然可以阅读

第一个:我只需要从我的公用文件夹中删除所有预编译的资产.

First one: I just had to delete all the precompiled assets from my public folder.

更好的方法之一:将config.serve_static_assets = false添加到development.rb,这将阻止从/public/assets加载文件.另外,别忘了重置浏览器缓存.

Better one: Add config.serve_static_assets = false to development.rb which will prevent loading files from /public/assets. Also, don't forget to reset your browser cache.

进阶课程:由于这些静态资产,我最近遇到了一个新问题.您知道,当您使用回形针或其他一些gem时,它们会将图像添加到某个系统子文件夹中的公用文件夹中,因为如果要使用capistrano部署应用程序会更好.好吧,那太好了!正如我们添加的config.serve_static_assets = false一样,这些图像未在开发中呈现,如果您要对它们进行一些CSS处理,那就不好了.所以!那该怎么办?

Advanced one: I recently had a new problem because of those static assets. You know, when you use paperclip or some other gem and they add your images in your public folder in some system sub-folder because it's better if you want to deploy your application using capistrano. Well, that's great but! As we added config.serve_static_assets=false, those images aren't rendered in development and that's bad if you want to do some css on them. So! What to do then?

实际上,您必须像这样打开开发中的静态资产:

Well in fact you'll have to turn on static assets in development like so:

# Expands the lines which load the assets
config.assets.debug = true
config.serve_static_assets = true

然后要防止Rails两次渲染您的其他资产(预编译的资产),只需执行以下命令:

Then to prevent rails from rendering your other assets twice (the precompiled ones), just do this command:

rake assets:clean

与rake资产相反:预编译并清理您的public/assets文件夹,以使Rails不会两次渲染您的资产.当然,您仍然需要清理浏览器缓存,并在每次进行预编译时清理资产.

It's the opposite of rake assets:precompile and will clean your public/assets folder so that Rails won't render your assets twice. Of course you'll still have to clean your browser cache and clean your assets each time you precompiled them.

-来自@idejuan答案

- From @idejuan answer

另一种解决方案:

您可以添加以下行:

config.assets.prefix = '/dev/assets'

到development.rb,前缀可以是您想要的任何名称.脚本将不再加载两次,并且将读取/public/system中的图像!但是要小心,因为它会更改静态"资产的路径...如果您需要来自gem的资产,则可能无法在开发中正确加载它们...

To development.rb, where the prefix can be whatever you want. Scripts will not load twice anymore, and images in /public/system will be read! But be carefull as it changes the path to your "static" assets... if you require assets from a gem, it might not load them properly in development...

[结束编辑]

剩下的有答案的问题!

那么,为什么我的开发应用程序呈现静态的预编译资产?

Well, why my development application was rendering static precompiled assets?

实际上,如果您预先在本地编译资产,则Rails会默认从开发和测试环境中的公用文件夹以及资产文件夹中渲染资产.通常,公用文件夹中的资产应覆盖资产文件夹中的资产,但事实并非如此,即使这样做,我们也会失去"debug_mode"的好处,因为每次都必须预编译资产.因此,...在开发和测试环境中本地进行预编译时,资产将呈现两次.

In fact if you precompile your assets localy, rails render assets from the public folder AND from the assets folder in development and test environment by default. Normally assets from the public folder should overwrite those from the assets folder, but it's not the case and even if it does, we would lost the benefits of the "debug_mode" as we would have to precompile assets each time. So... Assets are rendered twice when precompiled locally in development and test environment.

因此,通过在您的development.rb文件中添加"config.serve_static_assets = false",您将以某种方式覆盖告诉Rails在公用文件夹中查找资产的默认行.我希望他们有一天能对本地预编译的资产做一些更清洁的事情.

So, by adding "config.serve_static_assets = false" to your development.rb file, you somehow overwrite the default line that telling Rails to look in your public folder for assets. I hope they'll do something cleaner one day about locally precompiled assets.

感谢那些帮助我进行调查的人:).

Thanks to the ones who helped me for my investigations :).

Kulgar.

推荐答案

您可能想看看

https://stackoverflow.com/a/7854902/686460

将config.serve_static_assets = false添加到development.rb将阻止从/public/assets加载文件"

"Adding config.serve_static_assets = false to development.rb will prevent loading files from /public/assets"

我认为这是比您在这里建议的更好的解决方案.

I think that is a better solution than what you are suggesting here.

这篇关于Ruby On Rails 3.1-资产管道-资产呈现两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆