マップをクリックしてホームページに移動したい場合 localhost:3000/maps はこのエラーを取得します No route matching {:action=>"show", :controller=>"restaurants"}
controllers/maps_controller.rb
def index
@maps = Map.all
@json = Map.all.to_gmaps4rails do |map, marker|
marker.infowindow info_for_restaurant(map.restaurant)
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @maps }
end
end
def show
@map = Map.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @map }
end
end
private
def info_for_restaurant(restaurant)
link_to restaurant_path do
content_tag("h2") do
restaurant.name
end
end
end
ルート.rb
resources :restaurants
resources :maps
これは私の質問に対する答えです:
controllers/maps_controller.rb
def index
@maps = Map.all
@json = Map.all.to_gmaps4rails do |map, marker|
marker.infowindow render_to_string(:partial => "/maps/maps_link",
:layout => false, :locals => { :map => map})
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @maps }
end
end
ビュー/マップ/_maps_link.html.erb
<div class="map-link">
<h2><%= link_to map.restaurant.title, map.restaurant %></h2>
</div>