How to get id field and send to view

I would like to know how I can get the ID field from my model for passing in JavaScript, which I call when I click the marker.

My model:

acts_as_gmappable :process_geocoding => false, :lat => "lat", :lng => "lon" 

My controller

 def index @locales_markers = Locale.all.to_gmaps4rails respond_to do |format| format.html { render :layout => false } end end 

My view

 <%= gmaps("markers" => { "data" => @locales_markers } %> <% content_for :scripts do %> <script> Gmaps4Rails.callback = function() { for (var i = 0; i < this.markers.length; ++i) { google.maps.event.addListener(Gmaps4Rails.markers[i].serviceObject, 'click', function(){ $('#info').load('/messages/' **<HERE I'D LIKE TO PASS THE ID>**); $('#info').dialog("open"); }); } }; </script> <% end %> 

Thanks Luciano

+1
ruby ruby-on-rails ruby-on-rails-3 google-maps gmaps4rails
source share
2 answers

Each property of the marker is saved and can be restored.

In your example, just do:

 $('#info').load('/messages/' + Gmaps4Rails.markers[i].id ); 

EDIT:

You must create another js function:

 custom_listener = function(the_id) { return function(){ $('#info').load('/messages/' + the_id ); $('#info').dialog("open"); }; }; 

And call it that:

 Gmaps4Rails.callback = function() { for (var i = 0; i < this.markers.length; ++i) { google.maps.event.addListener(Gmaps4Rails.markers[i].serviceObject, 'click', custom_listener(Gmaps4Rails.markers[i].id)); } }; 
+1
source share

I changed my controller to:

  def index @locales_markers = Locale.all.to_gmaps4rails do |locale| "\"id\": \"#{locale.id}\"" end respond_to do |format| format.html { render :layout => false } end end 

and work fine, but I cannot get the id value in the view.

How to solve it?

Thanks Luciano

+1
source share

All Articles