title: "How to dynamically serve a KML file with Ruby on Rails"
author: "Chris O'Sullivan"
Serving a KML file in Ruby on Rails is way easier than doing it in asp.net.
Here’s how I do it for my little display kml on a google map application.First of all, you want to trick Google into thinking that the kml file is just a regular file with the extension “kml”. I do this by using a custom route in routes.rb that looks a little something like this:
map.connect ':controller/:action/:ignore_this_bit/:uuid.:format #The :ignore_this_bit is optional if you want to get around the google #caching issue detailed belowSo that means you could serve the file with a url like this:
def get_kml @kml_data = KmlData.find_by_uuid(params[:uuid]) if @kml_data.kml send_data @kml_data.kml else render :text => 'Kml is empty' end end
The @kml_data.kml variable contains the raw xml formatted kml.
The real guts of the function is the send_data function. This is rubys way of sending a file to the browser, which is how google expects to view the file.
And that’s it! Piece of cake!