Topic: Using Gmaps4Rails with a Rails 2.3 application

Topic type:

The steps necessary to get the Google Maps for Rails gem, which is designed for Rails 3 and above, working with a Rails 2.3 application.

The Kete application has the option of providing Google Maps interfaces for both viewing location data and setting it. At the time of this writing, it uses Google Maps API version 2. Lately we've been getting strange errors due to version 2's depreciation in favor of version 3. So I'm working on upgrading Kete, which at the moment is a based on Ruby on Rails version 2.3.5, to use Google Maps API version 3. To that end, I'm switching Kete's custom code and use of YM4R to use the Google Maps for Rails gem.

Although there are many options for libraries that integrate Google Maps Javascript API Version 3 with Ruby on Rails, Google Maps for Rails looks to have the cleanest API, be the most used, and the best supported. @apneadiving seems particularly good about answering questions on Stack Overfow.

The difficulty is that Google Maps for Rails (also called gmaps4rails), is designed to be used as a Rails Engine on Rails 3 or above. The good news is that I've gotten it working with Rails 2.3.x with some workarounds. They aren't necessarily pretty, but they work. Here they are:

  1. install the gmaps4rails gem (use bundler or rvm gem sets or just plain gem install gmaps4rails) as it suits you
  2. require the necessary libraries and add Gmaps4railsHelper to ActionController::Base. Add this to a file in config/initializers/ (or in my case I added it to an existing file under lib/, you could also stick it in config/enviroment.rb) :
    require 'gmaps4rails/base'
    # skipping require 'gmaps4rails/acts_as_gmappable' as I don't need it for my project
    require 'gmaps4rails/extensions/array'
    require 'gmaps4rails/extensions/hash'
    require 'gmaps4rails/helper/gmaps4rails_helper'
    ActionController::Base.send :helper, Gmaps4railsHelper
  3. add the necessary yield statements to your appropriate layout head, see the gmaps4rails README for details
  4. override the gem's provided partial by creating one under your application's app/views/gmaps4rails/_gmaps4rails.html.erb file with the provided in this gist
  5. you'll need to manually include the necessary javascript libraries files in your code base. Here's what I did:
    cd public/javascripts
    mkdir gmaps4rails
    cd gmaps4rails
    curl -L https://raw.github.com/apneadiving/Google-Maps-for-Rails/master/public/javascripts/gmaps4rails/gmaps4rails.base.js -O
    curl -L https://raw.github.com/apneadiving/Google-Maps-for-Rails/master/public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js -O # note I skipped other providers (Bing, OpenLayers), as don't need them, add curl -L ... if you do
    cd ../../stylesheets
    curl -L https://raw.github.com/apneadiving/Google-Maps-for-Rails/master/public/stylesheets/gmaps4rails.css -O
  6. use Google Maps for Rails as you would normally by setting up your objects that have locations in your controller and calling the helper in a view (again, see the gem's README). In my case this meant I constructed JSON directly.

You should now have a shiny new Google Map! Enjoy.

I've skipped the version control commands I've used (i.e. git add javascripts/gmaps4rails, etc.). Use them as appropriate to your project.

Note: I haven't tested this with the acts_as_gmappable functionality in the model. I've only tested it by feeding JSON objects directly to the gmaps4rails view helper. So there may be additional steps to get acts_as_gmappable working.

Discuss This Topic

There are 0 comments in this discussion.

join this discussion

Like what you've read? You can check out my latest project at Generali.st.

Got a project of your own that you need help with? Let's talk