【问题标题】:Gem works in IRB but not Rails consoleGem 在 IRB 中有效,但在 Rails 控制台中无效
【发布时间】:2021-02-05 16:45:40
【问题描述】:

我正在尝试使用位于 https://github.com/HubSpot/hubspot-api-ruby#installation 的 Hubspot API Ruby gem,但我遇到了 Ruby on Rails 的问题。

对于我所有的其他 gem,我可以简单地将 gem 添加到 Gemfile 中,它可以在应用程序中工作,但不是这种情况,我不知道为什么。

例如,这是我的 Gemfile 中的内容:

# HubSpot API Client
gem 'hubspot-api-client'

我已经运行bundle install 并成功安装,但我仍然无法使用该 gem。如果我通过 irb 控制台加载 gem,那么它就可以正常工作。

这就是我的意思:

使用 IRB

root@8af3866a29c8:/myapp# irb
irb(main):001:0> require 'hubspot-api-client'
=> true
irb(main):002:0> Hubspot
=> Hubspot

使用 Rails 控制台

root@8af3866a29c8:/myapp# rails c
Running via Spring preloader in process 6589
Loading development environment (Rails 5.2.4)
irb(main):001:0> require 'hubspot-api-client'
Traceback (most recent call last):
        1: from (irb):1
LoadError (cannot load such file -- hubspot-api-client)
irb(main):001:0> Hubspot
Traceback (most recent call last):
        1: from (irb):1
NameError (uninitialized constant Hubspot)
irb(main):002:0> 

为什么会发生这种情况,我该如何解决?

【问题讨论】:

  • 如果你的 gemfile 中有它,除非你使用了require: false,否则你不需要在你的 Rails 控制台中手动要求它。
  • 即使以这种方式或其他方式(手动要求),它仍然不起作用,但这是一个我不知道的好技巧。谢谢
  • 无论如何......这似乎是一个更受欢迎的宝石 - github.com/HubspotCommunity/hubspot-ruby
  • 您是否成功地使用了 Rails 中的其他 gem?只是想确保您使用的是应用程序的版本而不是系统,尽管我认为这不太可能。也许试试bundle exec rails c 看看有没有什么不同?
  • 是的。 Rails 中的所有其他宝石都可以正常工作

标签: ruby-on-rails ruby rubygems


【解决方案1】:

您必须在 Gemfile 中包含 gem 'hubspot-api-client'。 Rails 只加载 Gemfile 中指定的 gem。

【讨论】:

  • 我就是这么做的。我在原始帖子中发布了它。
  • 你能分享一下 gem 'hubspot-client' 版本吗?
  • /usr/local/bundle/gems/hubspot-api-client-9.3.0
  • 我的机器上安装了相同的版本。捆绑更新后,我检查了 irb 和 rails 控制台,所以它工作正常,只有第一次返回 true 但加载后返回 false
  • 它也在 Rails 控制台中工作?您可以拨打Hubspot 并且有效吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多