Ruby On Rails 3.1 - 资产管道 - 资产渲染两次
大更新:
当我终于找到真正的解决方案时,我也发现了真正的问题。由于我在这里写下了很多无用的信息,考虑到真正的问题,我正在对问题进行大量更新,以便其他人可以轻松找到发生了什么并可以看到解决方案。
问题: 这是因为Rails 3.1的资产管道
实际上...这很简单:资产在开发环境中渲染了两次。大量调查表明我的 Rails 3.1 服务器正在渲染“app/assets”和“public/assets”文件夹中的资源。所以,我复制了每个 .js 和 .css 文件,这破坏了我所有的 javascript 动画(是的......将两次相同的事件和处理程序绑定到同一元素不是你想要的......通常)。
如果问题突然出现,那是因为我必须运行“rake assets:precompile”来部署我的应用程序。从那时起,当我的应用程序在开发中运行时,服务器正在渲染静态预编译资产和动态预编译资产。
解决方案(现在有更好的下面几行) - 但您仍然可以阅读它
第一个:我只需从公共文件夹中删除所有预编译资产。
更好的方法是:将 config.serve_static_assets = false 添加到development.rb,这将阻止从 /public/assets 加载文件。另外,不要忘记重置浏览器缓存。
[编辑:2012 年 7 月 20 日]
进阶一:由于这些静态资源,我最近遇到了一个新问题。您知道,当您使用回形针或其他一些 gem 时,它们会将您的图像添加到某些系统子文件夹中的公共文件夹中,因为如果您想使用 capistrano 部署应用程序会更好。嗯,那太好了,但是!当我们添加 config.serve_static_assets=false 时,这些图像不会在开发中渲染,如果您想对它们执行一些 css,那就不好了。所以!那该怎么办呢?
事实上,您必须在开发中打开静态资源,如下所示:
# Expands the lines which load the assets
config.assets.debug = true
config.serve_static_assets = true
然后,为了防止 Rails 渲染您的其他资源两次(预编译的资源),只需执行以下命令:
rake assets:clean
它与 rake asset:precompile 相反,并且会清理您的资源public/assets 文件夹,这样 Rails 就不会渲染你的资源两次。当然,您仍然需要清理浏览器缓存并在每次预编译资产时清理它们。
[编辑:2013 年 11 月 18 日] - 来自 @idejuan 的回答
另一个解决方案:
您可以添加以下行:
config.assets.prefix = '/dev/assets'
到开发中。 rb,其中前缀可以是您想要的任何内容。脚本将不再加载两次,并且 /public/system 中的图像将被读取!但要小心,因为它会更改“静态”资产的路径...如果您需要来自 gem 的资产,它可能无法在开发中正确加载它们...
[编辑结束]
剩下的问题及答案!< /strong>
那么,为什么我的开发应用程序要渲染静态预编译资源?
事实上,如果您在本地预编译资产,则默认情况下,rails 会从公共文件夹以及开发和测试环境中的资产文件夹中渲染资产。通常,公共文件夹中的资产应该覆盖资产文件夹中的资产,但事实并非如此,即使这样做,我们也会失去“debug_mode”的好处,因为我们每次都必须预编译资产。所以...在开发和测试环境中本地预编译时,资源会渲染两次。
因此,通过将“config.serve_static_assets = false”添加到development.rb 文件中,您可以以某种方式覆盖告诉Rails 在公共文件夹中查找资产的默认行。我希望有一天他们会对本地预编译资产做一些更干净的事情。
感谢那些帮助我进行调查的人:)。
库尔加尔。
Big Update:
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.
The Problem:
It's because of the assets pipeline of Rails 3.1
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).
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.
The solution (there's now a better one few lines below) - but you can still read it
First one: I just had to delete all the precompiled assets from my public folder.
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.
[Edit: July 20th 2012]
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
Then to prevent rails from rendering your other assets twice (the precompiled ones), just do this command:
rake assets:clean
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.
[Edit: November 18th 2013] - From @idejuan answer
Another solution:
You can add this line:
config.assets.prefix = '/dev/assets'
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...
[End edit]
The remaining question with answer!
Well, why my development application was rendering static precompiled assets?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能需要查看
https://stackoverflow.com/a/7854902/686460
“添加config.serve_static_assets = false development.rb 将阻止从 /public/assets 加载文件”
我认为这是比您在这里建议的更好的解决方案。
You might want to look at
https://stackoverflow.com/a/7854902/686460
"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.
我遇到了同样的问题,并找到了一个简单的解决方案/破解方法。我刚刚添加了这一行:
到development.rb,其中前缀可以是您想要的任何内容,我将 /dev/assets 放在以避免进一步混淆。
脚本不再加载两次,并且我可以使用 /public/system 中的图像,当我尝试 config.serve_static_assets = false 的解决方案时无法读取该图像
到目前为止,我还没有发现该解决方案的缺点。希望它也能为您服务!
I had the same problem and found an easy solution/hack to it. I just added this line:
to development.rb, where the prefix can be whatever you want, I put /dev/assets to avoid further confusion.
Scripts do not load twice anymore, and I can use the images I have in /public/system that could not be read when I tried the solution of config.serve_static_assets = false
So far I have not found disadvantages to this solution. Hope it serves to you as well!
我不明白为什么 Ubuntu 升级会与你的 JavaScript 有任何关系。你使用静态 JavaScript 吗?动态 JavaScript?咖啡脚本?如果是后两者之一,我想升级可能破坏了处理 JS 的工具之一......
可以尝试一些事情:
I can't see why an Ubuntu upgrade would have anything to do with your JavaScript. Are you using static JavaScript? Dynamic JavaScript? CoffeeScript? If one of the latter two, I suppose it's possible that the upgrade broke one of the tools that's processing your JS...
Some things to try:
我还建议您运行 Rails 服务器并将其传递到不同的环境。这可能与您为开发设置的环境设置有关。
如果问题消失,则与“开发环境”的配置设置有关。
I would also suggest that you run the rails server passing it a different environment. This could be related to the Environment settings you have set up for development.
If the problem disappears it has got to do with your config settings for the "development environment.