- Create some
lib/helpers.rb
file with the contents:module Helpers; end
- Move the
bayarea_cities
method into this module definition - Add a
config/initializers/helpers.rb
file containingrequire "helpers"
- Inside the
ApplicationHelpers
class, addinclude Helpers
- Inside your
API_V2
class addinclude Helpers
You'll now have told Rails to make the Helpers
module available within your application, and made bayarea_cities
available as a method within both your Grape API class and your Rails app. The above are steps simply to get the point across - you need to put this common functionality in a place it can be easily accessed by any part of your application. You can (and should) use namespace your Helpers
module.
Another tip: add extend self
to the module to avoid the need to define everything as class methods as you mentioned in the comment
module Helpers
extend self
def bayarea_cities
#...
end
end
Finally, if you're including the module properly with include Helpers
, you should be able to acces the method simply as bayarea_cities
, not Helpers.bayarea_cities
. If this isn't the case, you should definitely show the error you get so we can sort that out for you.