在 Heroku 上加载 lib 目录中的模块
我在 Rails 3 项目的 lib 目录中有一个名为 slot.rb 的文件。它创建一个名为 SojournerSupport 的模块,并包含一个名为 Slot 的类。我有以下 config/application.rb 文件:
config.autoload_paths += %W(#{config.root}/lib)
然后,我将模块包含在我需要的模型中,如下所示:
include SojournerSupport
这在我的计算机上本地工作正常,但是当尝试在 heroku 中运行应用程序时,它显示“未初始化的常量 ShipRecord ::SojournerSupport (NameError)" ShipRecord 是我包含该模块的模型的名称。
有什么想法或建议吗?
I have a file in the lib directory of a rails 3 project called slot.rb. It creates a module called SojournerSupport, and contains a class called Slot. I have the following my config/application.rb file:
config.autoload_paths += %W(#{config.root}/lib)
I then include the module in the model that I need it like so:
include SojournerSupport
This works fine locally on my machine, but when trying to run the app in heroku it says "uninitialized constant ShipRecord::SojournerSupport (NameError)" ShipRecord being the name of the model that I include the module in.
Any ideas or suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在让我的模块在 Heroku 上运行时遇到了类似的问题。除了自动加载命名约定之外,我发现由于 Heroku 上的 Rails 生产环境做出的线程安全假设,必须需要模块代码(尽管它在我的 production.rb 文件中被注释掉了。)我在调用模块上的“include”之前“需要”模块文件,一切都开始工作。
请看一下这篇关于如何在 Heroku 中正确加载模块的优秀文章:
http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-product/
I had a similar problem with getting my module to run on Heroku. In addition to the autoload naming convention, I found out that the module code must be required due to a threadsafe assumption made by the Rails' production environment on Heroku (even though it was commented out in my production.rb file.) As soon as I "required" the module file before for calling "include" on the module, everything started to work.
Please take a look at this excellent article on the subject of getting Modules to load correctly in Heroku:
http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/
与其说这是一个解决方案,不如说是一种获取更多信息的方法,但是您是否尝试过从本地计算机上使用生产配置运行 Rails(使用
rails Production
)?如果您在那里遇到同样的问题,我会调查您的 production.rb 文件,看看它是否对您的自动加载路径做了任何奇怪的事情。
您还可以将控制台加载到生产环境(或 heroku 控制台),并调查您的 Application.config 以确保加载路径符合您的预期。
最后,当我看到 Heroku / dev 行为不一致时,我首先检查的事情之一是确保我使用的是匹配版本的 ruby。 Heroku 仍然或最近使用 1.8.7 作为默认值。
Not so much a solution as a way to get more information, but have you attempted to run rails with your production config from your local machine (using
rails s production
)?If you run into the same problem there, I would investigate your production.rb file to see if it's doing anything strange with your autoload paths.
You could also load a console to your production environment (or a heroku console), and investigate your Application.config to ensure that the load paths are as you expect.
Finally, one of the first things I check when I'm seeing inconsistent Heroku / dev behavior is to ensure that I'm using a matching version of ruby. Heroku either still is or was recently using 1.8.7 as the default.
如果 SojounerSupport 是顶级类或模块,那么您可能需要在包含它时明确声明它。
If
SojounerSupport
is a top level class or module, then you might need to explicitly state it is so when including it.实际上设法找出解决方案。我不确定为什么 heroku 在索引页面崩溃,但是当我点击使用模型拉入库模块的控制器时,它也会给我错误。 config.autoload_paths 似乎期望文件名与模块名称相同(如此处所述:Rails 3 库在需要之前不会加载)。我在 lib 目录中重命名了文件名,现在它可以在本地工作了。当我回到家时,我会推送到heroku,但我认为这解决了问题:)。
Actually managed to figure out the solution. I'm not sure why heroku was crashing at the index page, but when i hit a controller that utilizes a model pulling in the library module, it would give me the error as well. It would seem that the config.autoload_paths expects the filename to be the same as the module name (as noted here: Rails 3 library not loading until require). I renamed the filename in the lib directory as such, and now it's working locally. I'll push to heroku when i get home, but I think this fixed the issue :).