【问题标题】:Twilio library doesn't work in Rails, but works outside rails?Twilio 库在 Rails 中不起作用,但在 Rails 之外工作?
【发布时间】:2014-06-19 01:58:58
【问题描述】:

第一次发帖(请客气,我已经研究过了!)

我是来自另一种语言的 Ruby 生态系统的新手,但这让我觉得很奇怪。我已将 twilio-ruby 库添加到我的 Gemfile,完成捆绑安装/尝试捆绑更新

捆绑显示 twilio-ruby ~/.rvm/gems/ruby-2.1.2/gems/twilio-ruby-3.11.5

但是,在尝试 任何 示例时,我收到以下错误。

Undefined method 'messages' for #<Twilio::REST::Client::Account:0x00000102cea740>

替代呼叫等。没有任何作用。甚至没有@client.account.sms.messages(遗留电话)。

我熟悉 pry 并且已经完成了 @client.pry 产生 self.account.methods.sort 并且这些方法/类成员根本不存在于模块中。

那么有什么作用呢? ruby test.rb 包含:

@client = Twilio::REST::Client.new '{{removed}}', '{{removed}}'
#@client.pry


p @client.account.messages.create(
  :from => '+1XXX',
  :to => '+1XXX',
  :body => 'Hey there!'
)

如果我将相同的代码放在 application.rb 的顶部,在应用程序类声明之外它可以工作。

我还尝试过哪些其他地方?控制器、模型、在控制器/模型中导入和调用的独立类。似乎在 Rails 引导后它不起作用。我很困惑。

我很擅长调试,所以任何关于这个方向的提示都将不胜感激!

版本:Ruby 2.1.2、Rails 4.0.4、twilio-ruby 3.11.5

每次更改后,我都会重新启动 Rails 服务器。

更新

config/application.rb

require 'twilio-ruby'
@client = Twilio::REST::Client.new '{{removed}}', '{{removed}}'
#@client.pry


p @client.account.messages.create(
  :from => '+',
  :to => '+',
  :body => 'Hey there!'
)

Bundler.require(:default, Rails.env)

#require 'twilio-ruby'
@client = Twilio::REST::Client.new '{{removed}}', '{{removed}}'
#@client.pry


p @client.account.messages.create(
  :from => '+',
  :to => '+',
  :body => 'Hey there!'
)

在调用 bundler 之前调用 twilio-ruby 有效。

在调用 bundler 之后调用 twilio-ruby 不起作用。

【问题讨论】:

  • 这绝对是奇怪的(对我来说不可复制)。如果您启动一个空的 Rails 项目,只添加 twilio-ruby gem,这种情况还会发生吗?
  • 如果您取消注释require 'twilio-ruby' Bundler.requre 调用之后是否有效?
  • 试试@client.account.sms.messages.create
  • 你还有这个问题吗?我写了两篇关于将 Twilio 集成到 Rails 4.x 应用程序中的详细博客文章:Part 1Part 2

标签: ruby-on-rails twilio


【解决方案1】:

6 年后,我正在分享这个问题的罪魁祸首:春天

我跑了spring stop,突然一切都按预期加载了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 2012-02-27
    • 2017-03-19
    • 2015-08-16
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多