在 Heroku 上预编译资产时如何普遍跳过数据库接触 [英] How to universally skip database touches when precompiling assets on Heroku

查看:29
本文介绍了在 Heroku 上预编译资产时如何普遍跳过数据库接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Rails 3.1 应用程序部署到 Heroku 的 Cedar 堆栈.使用 Heroku Cedar 和 Rails 3.1,你可以自己在本地编译资产,让 Heroku 在你推送时编译它们(在slug编译"),或者让它们在应用程序运行时即时编译.我想做中间选项,让 Heroku 预编译资产.

I'm deploying a Rails 3.1 app to Heroku's Cedar stack. With Heroku Cedar and Rails 3.1, you can compile the assets yourself locally, let Heroku compile them when you push (during "slug compilation"), or have them be compiled just-in-time while the app is running. I want to do the middle option, letting Heroku precompile the assets.

当 Heroku 运行 assets:precompile 任务时,它会出现 无法连接到服务器" 的错误,因为应用程序正在尝试连接到数据库,但在 slug 编译阶段没有可用的数据库.此时缺少数据库连接是意料之中且不可避免的.我正在寻找一种方法来绕过它,因为数据库连接对于资产预编译并不重要.

When Heroku runs the assets:precompile task, it errors with "could not connect to server" because the app is trying to connect to the database but no database is available at that stage of slug compilation. The lack of database connection is expected and unavoidable at this point. I'm looking for a way to move past it, because a database connection isn't crucial to asset precompilation.

我的应用程序中尝试连接到数据库的部分是设计.在routes.rb 中有一个devise_for :users 行想要查看用户模型.

The part of my app that's trying to connect to the database is Devise. There's a devise_for :users line in routes.rb that wants to look at the User model.

我可以编写一个 rake 任务,将 devise_for 存根并使其成为资产的先决条件:预编译.我认为这会解决我的问题,但是我正在寻找一个更通用的解决方案,我可以在 任何 Rails 3.1 应用程序上使用它在 Heroku 上解决这个问题.

I could just write a rake task that stubs out devise_for and make it a prereq of assets:precompile. I think that would solve my problem, but I'm looking for a more universal solution that I could use on any Rails 3.1 app with this problem on Heroku.

有没有什么东西,或者你能想到什么可以消除数据库连接错误,同时仍然运行足以生成路线和资产路径的应用程序吗?

Is there anything out there, or can you conceive of anything that silences database connection errors while still running the app enough to have route and asset path generation?

显然,如果应用程序需要在启动期间读/写数据,我们不能存根,但我们可以自动伪造每个 ActiveRecord 模型吗?

Obviously if an app needs to read/write data during startup, we can't stub that, but can we fake every ActiveRecord model automatically?

推荐答案

此答案已过时且不再有效 - 请参阅 fringd 的答案.

This answer is out of date and no longer works - See fringd's answer.

不是一个通用的存根,但设计现在添加了一个检查来解决这个特定问题.请参阅问题修复.通过提供 RAILS_ASSETS_PRECOMPILE 环境配置设计应该跳过构建路线

Not quite a universal stubbing but devise has added a check now to fix this particular problem . See the issue and fix on Github. By providing a RAILS_ASSETS_PRECOMPILE environment config devise should skip building the routes

这篇关于在 Heroku 上预编译资产时如何普遍跳过数据库接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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