首页 > 解决方案 > Rails NoMethodError(Hyperclient:Module 的未定义方法“新”)

问题描述

这是一个相当“新手”的问题,但我一直在搜索,似乎找不到任何东西来解决在我的模型方法之一中使用超级客户端在 Rails 应用程序中出现的错误。

每当我运行时,rails c我都会收到错误 NoMethodError (undefined method `new' for Hyperclient:Module)

在我的宝石文件中

gem "hyperclient"

——跑了bundle install

在模型app/models/artwork.rb

class Artwork < ApplicationRecord
    require "hyperclient"

    def self.test_artsy
        api = Hyperclient.new('https://api.artsy.net/api') do |api|
          puts api
        end
    end
end

我查看了hyperclient上的文档,发现他们确实有 .new 的方法(也在他们的示例中),所以我知道这是我做错了,但我不确定还有什么可以尝试的?

在终端中打开 irb 时,代码确实有效。

标签: ruby-on-railsrubygems

解决方案


您还没有在您的文件中指定任何版本,Gemfile因此可能会发生Bundler不会安装最新版本的情况,这就是我猜这里可能发生的情况。

您可以通过执行以下操作检查安装了哪个版本: cat Gemfile.lock | grep hyperclient

您在这里至少有两个选择:

1) bundle update hyperclient- 这应该将您更新Gemfile.lock到最新版本

2) bundle remove hyperclient bundle add hyperclient --version "0.9"- 这将更新您的 Gemfile,然后运行bundle install它将重新生成您的Gemfile.lock

建议使用第二个选项,因为始终为您使用的 gem 指定版本是一个很好的做法。


推荐阅读