Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby/Rails - How to Create a Class and Access it from the Controller

I've been trying to use a few different gems for displaying googlemaps within rails 3 and have had several problems.

Luckily I found this https://github.com/YouthTree/bhm-google-maps which is a helper and it seems to work for others.

I've installed it properly but in the readme https://github.com/YouthTree/bhm-google-maps/blob/master/README.md it mentions creating a class for the object to display in the view.

The example they gave was

class Location
   attr_accessor :address, :lat, :lng
   def initialize(address, lat, lng)
      @address = address
      @lat = lat
      @lng = lng
   end
  def to_s; address.to_s; end
end

And then running

 <%= draw_map_of Location.new("My House", 12.345, 56.789) %>

in the view.

It seems simple enough but I haven't experienced the need of creating a class before in rails so I have some questions.

Should I create a location.rb file and place the above code in it, but where should I place the file? (model folder, application folder????)

Is there a way for me to create this class within my controller?

Ideally I would like to manipulate the lat/lng values as variables and display a dynamic map.

like image 379
ChrisWesAllen Avatar asked Mar 02 '11 22:03

ChrisWesAllen


People also ask

How do I create a controller in Rails?

To generate a controller and optionally its actions, do the following: Press Ctrl twice and start typing rails g controller. Select rails g controller and press Enter . In the invoked Add New Controller dialog, specify the controller name.

What does controller do in Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.

What does Before_action do in Rails?

When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.

What is Before_filter in Ruby on Rails?

before_filter and around_filter may halt the request before a controller action is run. This is useful, for example, to deny access to unauthenticated users or to redirect from HTTP to HTTPS. Simply call render or redirect.


1 Answers

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.

like image 197
dnch Avatar answered Oct 05 '22 11:10

dnch