Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom classes in rails

I'm having difficulties to add a custom class to my app.

It's a class that spiders a website and returns 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!

like image 627
Tim Baas Avatar asked Jun 09 '11 12:06

Tim Baas


1 Answers

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

like image 118
Jeremy B. Avatar answered Sep 30 '22 13:09

Jeremy B.