Search code examples
ruby-on-railsgoogle-mapsruby-on-rails-3dom-eventsgmaps4rails

Place a link within a infowindow using gmaps4rails


I am using gmaps4rails and now when I click on a marker appears information from the database, I need a putting a link inside the marker. How do I?

Model:

def gmaps4rails_address
  city
end

def gmaps4rails_infowindow
   "<h4>#{city}</h4>"  
end

Controller:

def index
  @postos = Posto.all
  @markers = Posto.all.to_gmaps4rails  

  @json = Posto.all.to_gmaps4rails  do |posto, marker|
    marker.json "\"id\": #{posto.id}"
  end  
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @postos }
  end
end

Solution

  • I don't recommend you use the gmaps4rails_infowindow method: view details shouldn't be given at the model layer.

    You should rather configure the infowindow in the controller, using a partial:

    @json = Posto.all.to_gmaps4rails do |posto, marker|
      marker.infowindow render_to_string(:partial => "/path_to/your_template", :locals => { needed_locales })
    end
    

    Details are in the gem's wiki. (you could even use js templates but it's not the question and it's explained in the wiki as well)