Search code examples
ruby-on-railsruby-on-rails-3.1gmaps4rails

Not able to render partial with Gmaps4Rails


I have a map which has got marker based on state of US. Each state has n number of city.

I have got a state model, controller and city model, controller.

When I click on the marker of the state, I want the list of cities to be displayed in the info window.

All this information is appearing on the homepage.

This is what I have done so far :-

home_controller.rb

def index
    @states = State.all.to_gmaps4rails do |state,marker|
        marker.infowindow render_to_string(:partial => "/states/gmaps4rails_infowindow", :locals => {:object => state})
        marker.json({:id => state.id})
    end
end

home/index.html.haml

=gmaps({"map_options" =>{ "auto_zoom" => false, "zoom" => 3}, "markers" => { "data" => @states } })

state_controller.rb

def gmaps4rails_infowindow
  @state = Gmaps.map.markers
end

states/_gmaps4rails_infowindow.html.haml

[email protected] do |city|
    =city.name

Needless to say that it is not working. Can someone please help me out?


Solution

  • Well, your home_controller.rb is fine. you write here you want to use a partial with a local variable named object.

    In the partial itself, you write:

    [email protected] do |city|
      =city.name
    

    The instance variable isn't defined there, you defined a local variable just above.

    Replace with:

    =object.cities.each do |city|
      =city.name
    

    From there it should work.


    Notice:

    def gmaps4rails_infowindow
      @state = Gmaps.map.markers 
    end
    

    is:

    • useless: you define the infowindow in the controller

    • wrong: Gmaps.map.markers only lives as js variable