gmaps4rails — заполнение информационного окна несколькими значениями из массива

Я использую гем gmaps4rails, и я хотел бы, чтобы информационное окно для маркера перечисляло все значения в массиве recipes в приведенном ниже коде.

class RecipesController < ApplicationController
  include ApplicationHelper

  def index

  end

  def home
    @countries = Country.where user_id: current_user.id

    @hash = Gmaps4rails.build_markers(@countries) do |country, marker|

      recipes = country.recipes

      marker.lat country.latitude
      marker.lng country.longitude

      marker.infowindow # want to list recipes.each here

    end
  end
end

любая помощь приветствуется.


person i_trope    schedule 14.01.2014    source источник


Ответы (2)


Вы можете просто установить переменную внутри блока, например:

  @hash = Gmaps4rails.build_markers(@countries) do |country, marker|

  country_recipes = Array.new
  country.recipes.each do |recipe|
      country_recipes.push(recipe.name)
  end

  marker.lat country.latitude
  marker.lng country.longitude

  marker.infowindow country_recipes

end

Вот аналогичный пример того, что я сделал в своем собственном приложении:

    @ticket_properties = Array.new
    @ticket.properties.each do |property|
      @ticket_properties.push(property)
    end  

    @hash = Gmaps4rails.build_markers(@ticket_properties) do |ticket_prop, marker|
        concat_info_window = "#{ticket_prop.name}, #{ticket_prop.address}"
        marker.lat ticket_prop.latitude
        marker.lng ticket_prop.longitude
        marker.infowindow concat_info_window
    end

Позвольте мне знать, если это помогает.

person Nubtacular    schedule 02.02.2016

Я думаю, вы можете попробовать это:

# render the content to a html string, then pass it to the infowindow method
marker.infowindow render_to_string("recipes/marker_info", :locals => { :recipes => recipes}, :layout => false, :formats => :html)

# recipes/marker_info.html.erb
<ul>
  <% recipes.each do |recipe| %>
    <li><%= recipe.value # or other methods %></li>
  <% end %>
</ul>
person Tony Han    schedule 14.01.2014