如果我想通过我的主页点击地图 localhost:3000/maps 会出现此错误 No route matches {: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>