Ruby/Rails - How to Create a Class and Access it from the Controller
You should put location.rb
wherever you feel it makes the most sense. Having it at app/models/location.rb
will ensure that it's automatically required when your app starts, but some people expect that classes in app/models
are backed by ActiveRecord.
You could also put it under lib/
if you prefer.
To make it available to the app, you can include require statement in project initializers inside your config folder:
require "#{Rails.root}/lib/location.rb"
As for creating it inside your Controller - definitely! It's just another instance of a class:
def show @location = Location.new("My House", 12.345, 56.789)end
And then in your view:
<%= draw_map_of @location %>
Don't forget – beneath Rails is all the power and flexibility of pure Ruby, ready to be used. You're not only limited to what Rails gives you.