2016-03-28 90 views
1

我在尝试调用控制器中的方法时遇到错误。关于如何使这个工作有效的教程,但只是有点卡在泥里,需要一些帮助。控制器中的未定义方法

NoMethodError in CatalogController#index 
undefined method `art' for #<Class:0x007fbe8c338310> 

我的模型

require 'httparty' 
require 'json' 

class Feed < ActiveRecord::Base 
    include HTTParty 
    base_uri 'https://www.parsehub.com/api/v2/runs' 
    # GET /feeds 
    # GET /feeds.json 
    def art 
    response = self.class.get("/tnZ4F47Do9a7QeDnI6_8EKea/data?&format=json") 
    @elements = response.parsed_response["image"] 
    @parsed = @elements.collect { |e| e['url'] } 
    end 

end 

我控制器

class CatalogController < ApplicationController 


    def index 
     @images = Feed.art 
    end 


end 

我猜测它的东西很简单,我忘了。

回答

2

def art定义了一个实例方法,而不是一个类方法。

您有两个选项来解决这个问题:

1)加入self.的定义制作方法的类方法:

def self.art 
    # ... 

2)或之前在你的控制器创建一个Feed实例致电art

def index 
    @images = Feed.new.art 
end 
相关问题