Monday, June 18, 2012

Gmaps for rails 3

rails new gmaps

cd gmaps

rake db:create

rails g scaffold Location name:string address:string longitude:float latitude:float

In  app/view/layout,add the below
<!DOCTYPE html>
<html>
<head>
<%= yield :head %>
<title>GMaP</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
<%= yield :scripts %>
</html>



In app/model/location.rb,add the below
class Location < ActiveRecord::Base
acts_as_gmappable
def gmaps4rails_address
address
end
def gmaps4rails_infowindow
“<h4>#{name}</h4>” << “<h4>#{address}</h4>”
end
end

In app/controllers/locations_controller.rb,add the below
def index
@locations = Location.all
@json = Location.all.to_gmaps4rails

In app/views/locations/index.html.erb,add the below
<%= gmaps4rails(@json) %>

In Gemfile,add the below
gem ‘gmaps4rails’,’0.7.7′

bundle install

rake db:migrate

rails s

http://localhost:3000/locations

2 comments:

Unknown said...

This was awesome chimbu,pls continue posting like this.Very useful

Unknown said...

Is there any more optimized code than this for this UI visiblity.