I'm having difficulties to add a custom class to my app.
It's a class that spiders a website and retu开发者_C百科rns the results.
What i've found out is that I need to put it in the lib folder, I already pointed the autoload paths to the lib folder.. This is where I put it:
# /lib/booking_spider.rb
class BookingSpider
def cities( city )
return @cities
end
end
This is how I call it in my controller:
p BookingSpider.cities( params[:search][:city] )
This error keeps popping up:
undefined method `cities' for BookingSpider:Class
Can someone tell me what I'm missing here?
Thanks!
You are trying to use the method as a class method, but it is defined as an instance method. Change to this:
class BookingSpider
def self.cities(city)
return @cities
end
end
Here is some reading on the differences between class and instance methods: method types in Ruby
精彩评论