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

查看:31
本文介绍了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 assets: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.

进阶一:我最近遇到了一个新问题,因为那些静态资产.您知道,当您使用 paperclip 或其他一些 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 assets 相反:预编译并且会清理你的 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.

因此,通过将config.serve_static_assets = false"添加到您的 development.rb 文件,您会以某种方式覆盖默认行,该行告诉 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 :).

库尔加尔.

推荐答案

你可能想看看

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天全站免登陆