无法通过管道在本地生产Rails 3.1.3服务器上配置资产 [英] Cannot configure assets via pipeline on local production rails 3.1.3 server

查看:91
本文介绍了无法通过管道在本地生产Rails 3.1.3服务器上配置资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个月,我从Rails 3.0升级到Rails 3.1-这个星期我试图以生产模式启动服务器-今天我遇到了麻烦!

This month, I upgraded from Rails 3.0 to Rails 3.1 - this week I tried to launch the server in production mode - today I have hit a wall !

我无法通过资产管道获取生产环境服务器来提供公共资产(JavaScript和CSS),除非 我在我的environment.rb文件中设置了config.assets.compile = true,速度的原因,我显然不想这么做.

I am unable to get my production environment server to serve up my public assets (JavaScript and CSS) via the asset pipeline, unless I set config.assets.compile = truein my environment.rb file, which for speed reasons I obviously don't want to do.

我有大量的JS和CSS文件,每个文件都倾向于在一个或两个不同的页面上使用.这意味着创建单个清单"文件不适合我的用法,因为每个页面都需要稍微不同的内容.我还希望某些CSS不会很好地放在一起.因此,我放弃了只是让它正常工作"的方法,而是希望以后整理大量的CSS/JS.

I have a large number of JS and CSS files, each of which tends to get used on one or two different pages. This means creating a single "manifest" file doesn't fit my usage, as each page wants something slightly different. I also expect some of the CSS won't sit together nicely. Therefore I have gone down the approach of "just get it working", looking to tidy up the large quantity of CSS / JS later.

这是production.rb文件:

Here is the production.rb file:

Implicit::Application.configure do
  ...

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # I set this to true, as I am testing this locally, just running a local Thin server
  config.serve_static_assets = true

  config.assets.compress = true

  # Setting this to false removes the issue - but is SLOW
  config.assets.compile = true

  config.assets.digest = true

  # This is overkill - but does get EVERYTHING precompiled for now
  config.assets.precompile += %w( *.css *.js )

  config.action_dispatch.x_sendfile_header = nil
  ...
end

这对我来说是一个新领域,因此,我今天花了很多时间来切换这些布尔值,并停止/启动本地Thin/Rails服务器来进行尝试.但是唯一可以改变的值是编译标志.

This is quite a new area for me, and so I've spent much of today toggling these booleans and stop/starting the local Thin / Rails server to try them out. But the only value that's made a solid difference is the compile flag.

我的application.rb文件非常标准,并且其中包含config.assets.enabled = trueconfig.assets.initialize_on_precompile = false,后者来自heroku帖子(再次是猜测).

My application.rb file is pretty much standard, and has config.assets.enabled = true and config.assets.initialize_on_precompile = false in it, the latter from a heroku post (and a guess, again).

我有一个完全填充的public/assets目录,该目录是通过bundle exec rake assets:precompile命令创建的,大约需要20分钟才能在老式的笔记本电脑上运行(5年),这可能与捕获所有"预编译正则表达式有关,尽管在该行中添加了注释,但仍然需要10分钟以上(!)

I have a fully populated public/assets directory, created with the bundle exec rake assets:precompile command, that takes about 20 mins to run on my oldish laptop (5 years), probably something to do with that "catch all" precompile regex, although with that line commented it still takes over 10 mins (!)

在将编译标志设置为true的情况下,我可以看到在我的/tmp/cache目录中创建了这些资产的副本-显然,这是创建资产自己的已编译副本"的应用程序.

With the compile flag set to true, I can see copies of these assets getting created in my /tmp/cache directory - this is clearly the application creating it's own "compiled copy" of the assets.

在将编译标志设置为false的情况下,我遇到了jquery.reveal isn't precompiled的错误消息(在日志中,除非我将请求设置为local,否则将在呈现的错误页面上看到它).但是,当我转到http://localhost:3000/assets/jquery.reveal.js时,javascript文件已送达​​.

With the compile flag set to false, I am confronted with the error message (in the logs, unless I set requests to local, then I see it on the rendered error page) of jquery.reveal isn't precompiled. However, when I go to http://localhost:3000/assets/jquery.reveal.js the javascript file is served up.

导致此问题的我的应用程序布局的所在行是:

The line of my application layout causing this is:

<%= javascript_include_tag "application", "jquery.reveal" %>

我尝试将jquery.reveal更改为jquery.reveal.js而不作任何更改.删除它可以修复索引页面,但是jquery.reveal功能当然不可用了!因此很明显,application.js可以正确提供.我只是不知道为什么没有jquery.reveal,因为我可以在public/assets目录中看到预编译的文件.

I have tried changing that jquery.reveal to jquery.reveal.js with no change. Removing it fixes the index page, except that the jquery.reveal functionality is gone of course ! So clearly the application.js is being served up correctly. I just can't figure out why jquery.reveal isn't, as I can see the precompiled files in the public/assets directory.

推荐答案

这是一个有趣的问题,我认为可能存在两个错误-您链接了一个错误,另一个错误:正在将文件编译为错误的错误姓名.可能值得尝试生成一个最小的测试用例,然后将其与错误报告一起提交.

This is an interesting issue, and I think there may be two bugs - the one you've linked and another: the file is being being compiled to the wrong name. It might be worth trying to generate a minimal test case that you can submit with a bug report.

此问题的解决方法-我怀疑这就是为什么很少有人出现问题的原因-使用辅助清单(仅通过清单链接库似乎是一种不断发展的最佳实践).

The workaround for this - and I suspect that this is why few people seem to have the problem - is to use a secondary manifest (linking libraries only via a manifest seems to be an evolving best-practice).

创建一个名为home.js的文件,并只需要一个文件即可.

Create one called home.js and require just that one file to it.

总体来说,这不是一个坏方法.这些额外的清单必须添加到预编译数组中(请参阅指南),并允许在多个页面或部分上共享多个库,而不必每次都链接它们.

This isn't a bad approach overall. These extra manifests have to be added to the precompile array (see the guide), and allow multiple libraries to be shared over several pages or sections without having to link them each time.

这篇关于无法通过管道在本地生产Rails 3.1.3服务器上配置资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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