The following error is raised when I try to use twilio-ruby gem "warning: LoadError: cannot load such file -- rack/media_type"

The following error is raised when I try to use twilio-ruby gem "warning: LoadError: cannot load such file -- rack/media_type"

在将 ruby 文件添加到我的 gemfile 并执行 $bundle install 之后,我目前正在尝试 require "twilio-ruby",但是在尝试加载时我不断收到以下错误消息文件在 irb < warning: LoadError: cannot load such file -- rack/media_type >

错误路径返回 .rvm/rubies/ruby-3.1.1/lib/ruby/3.1.0/irb/init.rb:397

我的 gemfile 目前看起来像

source 'https://rubygems.org'

ruby '3.1.1'


group :test do
  gem 'rspec'
  gem 'simplecov', require: false, group: :test
  gem 'simplecov-console', require: false, group: :test
end

group :development, :test do
  gem 'rubocop', '1.20'
  gem 'twilio-ruby', '~> 5.61', '>= 5.61.1'
end

我的代码如下所示:

require 'rubygems'
require 'twilio-ruby'

class SMS

  def send_sms(message)
    account_sid = ENV["TWILIO_SID"]
    auth_token = ENV["TWILIO_AUTH_TOKEN"]

    @client = Twilio::REST::Client.new account_sid, auth_token

        @client.messages.create(
      from: ENV["TWILIO_PHONE_NUMBER"],
      to: ENV['PHONE_NUMBER'],
      body: message
    )
  end

end

我已经尝试过使用和不使用 require 'rubygems',但它总是出现同样的错误

我已经检查过了,我的 gem list 确实包括 twilio-ruby (5.65.0)

我认为您在 twilio-ruby 中发现了错误!最近对 gem 进行了更新,更改了作为 gem 一部分的机架中间件中的某些行为。它导致 gem 需要 Rack 的一些行为,但是在 Rack 没有加载的情况下,就像你上面的代码,这会失败,因为 Rack 不是依赖项。

我刚刚完成了 a PR to fix this。同时要解决此问题,您可以在合并之前的更改之前将 twilio-ruby 的版本降级到 5.63.0,或者您可以安装 rack 作为依赖项。

图书馆每两周发布一次,最后一次发布是在 2 月 24 日。因此,如果此修复程序很快获得批准,它可能会进入此版本。