Trying to combine Users into the same marker using gmaps4rails
I can't figure out how to combine users into the same markers in
gmaps4rails. I feel like I'm somewhat close, but can't exactly figure out
what I'm doing wrong between using the gmaps4rails helper, converting to
JSON, and rendering by partial.
def map
distinct_locations = User.where("location IS NOT
NULL").select("DISTINCT(LOCATION)").map(&:location)
@markers = []
distinct_locations.each do |city|
temp_marker = User.where(:location => city).first.to_gmaps4rails
city_users = User.where(:location => city)
render_to_string(:partial => "/users/map_marker_info", :locals => {
:users => city_users})
@markers << temp_marker
end
@markers
end
As you can see, in my controller, I first get an array of
distinct_locations, which is just an array of city names (like "New York,
New York").
Then I create the @markers to hold the JSON I'll eventually return.
For each unique city that I found, I'm TRYING to first create a marker
using the to_gmaps4rails helper using the first User I find in that city.
Then I'm trying to find all the other users in the city (via city_users =
User.where(:location => city).
I'm then trying to render my partial using this collection of Users in the
same city with something like this for now (but eventually will pull
different attributes from Users):
#_map_marker_info.html.haml
.map_info_window
%h4
=users.first.location
%p
=users.count
Finally, I add the marker to @markers after each city is processed.
Eventually, I return @markers as a JSON that then becomes a beautiful map
thanks to the awesome gmaps4rails gem.
I'm sure I'm doing a lot of things wrong (and probably many things
inefficiently)...but can someone help me just to get it working?
No comments:
Post a Comment