包括 Gem 不工作
Include Gem Not Working
当我试图在我的控制器中包含一个 gem 时,我遇到了一个奇怪的错误,下面是我在控制器中使用的代码,我得到的错误是:
> undefined method `include' for <CatalogController:0x00000002427b90>
返回此错误的代码如下:
require 'httparty'
require 'json'
class CatalogController < ApplicationController
# GET /feeds
# GET /feeds.json
def index
# GRAB THE URL
include HTTParty
base_uri 'https://www.parsehub.com/api/v2/runs'
# PARSE THE RESPONSE
def art
response = self.class.get("/tnZ4F47Do9a7QeDnI6_8EKea/data?&format=json")
@elements = response.parsed_response["image"]
@parsed = @elements.collect { |e| e['url'] }
end
end
end
不太清楚为什么会返回此错误?
您不需要包含。事实上,你甚至不应该需要那个要求。我假设这是一个 rails 应用程序。如果您通过 Gemfile
安装了 httparty
gem,您可以在整个应用程序中简单地使用 httparty 进行调用。
同时将该 art
方法移到索引方法之外。
当我试图在我的控制器中包含一个 gem 时,我遇到了一个奇怪的错误,下面是我在控制器中使用的代码,我得到的错误是:
> undefined method `include' for <CatalogController:0x00000002427b90>
返回此错误的代码如下:
require 'httparty'
require 'json'
class CatalogController < ApplicationController
# GET /feeds
# GET /feeds.json
def index
# GRAB THE URL
include HTTParty
base_uri 'https://www.parsehub.com/api/v2/runs'
# PARSE THE RESPONSE
def art
response = self.class.get("/tnZ4F47Do9a7QeDnI6_8EKea/data?&format=json")
@elements = response.parsed_response["image"]
@parsed = @elements.collect { |e| e['url'] }
end
end
end
不太清楚为什么会返回此错误?
您不需要包含。事实上,你甚至不应该需要那个要求。我假设这是一个 rails 应用程序。如果您通过 Gemfile
安装了 httparty
gem,您可以在整个应用程序中简单地使用 httparty 进行调用。
同时将该 art
方法移到索引方法之外。