Trying to populate gmaps4rails with multiple json strings in one page

I hope I am asking this right, so please let me know if I'm way off.

The problem is trying to build a homepage that draws from multiple controllers, to display the nearest locations from multiple controllers, ie. food, businesses, ect.

Right now the individual listings pages have maps drawn from their respective

@json = Controller.all.to_gmaps4rails

How would I do something like :

@json = Controller1 Controller2 .all.to_gmaps4rails

I hope this isnt a noob question and I'm just having a bad day. Thanks guys!

edit 12.5.2011 @seanhill - this is one of the models, the other sections are very close to this format. First off, I wasn't even sure if my homepage requires it's own model, as it doesn't interact with the db at all, more pulling data from controllers that do the work. Thanks for the response Sean!

class Dining < ActiveRecord::Base
validates_uniqueness_of :name, :message => "already exists"

attr_accessible :name, :address, :cuisine, :latitude, :longitude, :about, :facebook, :twitter, :phone, :website
geocoded_by :address
after_validation :geocode, :if => :address_changed?

acts_as_gmappable :process_geocoding => false

def gmaps4rails_address
    "#{self.address}"
end

def gmaps4rails_infowindow
    "<h3>#{self.name}</h3><br /><h5>#{self.cuisine}</h5>"
end

def self.search(search)
    if search
        where('name LIKE ?', "%#{search}%")
    else
        scoped
    end
end

end

Answers


Try this

holder = Controller1.all
holder << Controller2.all
@json = holder.flatten.map{|h| {lng: h.longitude, lat: h.latitude, class: h.class.to_s}}.to_json

Make sure to change longitude and latitude based on your column names and use js to manipulate the markers based upon class.

As the @Sean Hill said you shouldn't be calling .all on controllers but I think you have a slight misunderstanding of how things are working. Assuming you have a Model called Dining and another called Shop, when you call Dining.all or Shop.all inside class DiningsController < ApplicationController, you are calling .all on either the Dining Model or the Shop Model not on the DiningsController.

The information you display through a controller is only limited by the methods you call in it although it is best practice ensure the main focus of the information displayed is related to the respective controller.

So what you are really trying to do is get the records from multiple models and group them together to display them in a single map.

With that said the answer should read something like this

holder = Dining.all # Takes all Dining records returned as an array and sets them to holder variable 
holder << Shop.all # Pushes the Shop records array into the holder with the dining records
holder.flatten!# Next we flatten the array so we only have a single array. 

# Then we use the map method to run the given code one time for each instance 
# in the holder array to extract the info we need. The results for every instance 
# in holder are returned in an array which we then convert to_json.
@json = holder.map{|h| {lng: h.longitude, lat: h.latitude, class: h.class.to_s}}.to_json  

@json1 = something.to_gmaps4rails
@json2 = something.to_gmaps4rails

@json = (JSON.parse(@json1) + JSON.parse(@json2)).to_json

I populated the map with my initial data of festivals, and then added the rides to it with javascript with this code,

<% content_for :scripts do %>
  <script type="text/javascript">

    Gmaps.map.callback = function() {

      $.getJSON('/rides_gmap', function(data){
          Gmaps.map.addMarkers(data);
        });

      }
  </script>
<%end%>

In the rides controller I had this

def rides_gmap
    @rides = Ride.all
    @json = @rides.to_gmaps4rails do |ride, marker|
      marker.infowindow render_to_string(:partial => "/rides/infowindow", :locals => { :ride => ride})
      marker.picture({
                'picture' => view_context.image_path("orange-dot.png"),
                'width'   => 20,
                'height'  => 20
               })
      marker.title "#{ride.address}"
      marker.json({:ride_id => ride.id, :ride_festivaltype => ride.festival.festivaltype 
    end
    respond_with @json
  end

I hope this helps.


Need Your Help

how can i show advanced custom fields plugin fields show in front end classipress form

php wordpress

i am using word press theme classipress, now i want to add multiple dynamic drop downs which relay on each other.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.