【问题标题】:Ruby error: cannot load such file -- rest-clientRuby 错误:无法加载此类文件——rest-client
【发布时间】:2017-09-07 00:42:05
【问题描述】:

我正在使用 Ruby on Rails 4。

我正在尝试

require 'rest-client'

在我的控制器中,以便我可以解析从表单获取的登录信息并将其发送到 API。

我可以验证 gem 是否已安装并且也在我的应用程序根目录上的 Gemfile 中。

但是,当我尝试在我的控制器中请求文件时,它仍然抛出“无法加载此类文件--rest-client”。

我用谷歌搜索了这个错误,我看到的大多数答案要么是 gem 没有安装,不在 Gemfile 中,要么是两者的组合。这里的情况也不是。

我的控制器是否由于某种原因无法访问 rest-client gem?我必须使用 rest-client,因为它是 API 所必需的。

这是我用来安装 gem 的那一行:

gem install rest-client

这是gem的主页:https://github.com/archiloque/rest-client
这只会将您重定向到https://github.com/rest-client/rest-client

我还应该注意,当我不使用 Rails 项目中的代码而只是在终端中运行命令时,它可以正常工作。

【问题讨论】:

  • 根据您需要做的 github 文档,需要带有下划线的“rest_client”。
  • 我做了同样的改变,结果错误。
  • 它显示在捆绑包中。重新启动服务器就可以了。谢谢安倍。

标签: ruby-on-rails ruby rest-client


【解决方案1】:

假设您使用的是https://github.com/rest-client/rest-client(因为您没有指定),您的require 行应该是

require 'rest-client'

根据README。另外,确保在将 gem 添加到 Gemfile 并运行 bundle 之后重新启动 Rails 服务器。

【讨论】:

  • 这不再适用
  • 好吧,它是在将近 3 年前发布的。如果您有更改以使其相关,请随时提出修改建议。
  • 好吧,你不能进行 1 个字符的编辑 :),请将其从 require 'rest_client' 更改为 require rest-client
【解决方案2】:

在终端中运行以下命令:

gem install rest-client

并使用require 'rest-client'。不用改成rest_client。

【讨论】:

    【解决方案3】:

    就我而言,此线程中的解决方案均无效
    起作用的是直接在Gemfile 中添加gem:

     gem 'rest-client'
    

    关闭rails server后,退出rails console并运行bundle install
    我再次打开了rails console,这次require 'rest-client' 完美运行

    【讨论】:

      【解决方案4】:

      首先确保您已在 gem 文件中安装了 gem 'rest-client', ~> 1.8.0。运行 bundle install 然后require 'rest_client'。这对我有用。

      【讨论】:

        【解决方案5】:

        对我来说,这是 bundle 的问题(我以为我已经安装了)。剧透警报,我没有,这就是我修复它的方法。我在运行 OS X Yosemite 的 Mac 上,我的终端版本是 Darwin Kernel Version 14.3.0:

        cd
        gem install bundler 
        

        cd
        sudo gem install bundler
        

        如果您遇到以下错误:

        错误:执行 gem 时 ... (Gem::FilePermissionError) 您没有 /Library/Ruby/Gems/2.0.0 目录的写入权限。

        最后,将您的需求行更改为:

        require 'rest-client'
        

        require 'rest_client'
        

        然后运行你的代码!

        【讨论】:

          【解决方案6】:

          试试require 'rest_client',而不是require 'rest-client'

          【讨论】:

            猜你喜欢
            • 2012-04-02
            • 1970-01-01
            • 1970-01-01
            • 2022-10-23
            • 2012-12-03
            • 1970-01-01
            • 1970-01-01
            • 2014-10-09
            相关资源
            最近更新 更多