Tim Baas
Tim Baas

Reputation: 6185

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!

Upvotes: 7

Views: 3968

Answers (1)

Jeremy B.
Jeremy B.

Reputation: 9216

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

Upvotes: 11

Related Questions