2011-04-16 287 views
1

我想在Rails中構建一個KML文件,我已經成功完成了這個工作,但現在我想提供一個KMZ格式,它將呈現index.kml文件並將其壓縮。這是我陷入困境的地方。我已經更新了MIME類型,如下所示。rails - 渲染模板和zip

Mime::Type.register_alias "application/vnd.google-earth.kml+xml", :kml 
Mime::Type.register_alias "application/vnd.google-earth.kmz", :kmz 

這裏是我的格式塊

def index 
    @map_items = Items.all 
    respond_with(@map_items) do |format| 
     format.kml 
     format.kmz { NOT SURE WHAT IS BEST TO DO } 
     format.georss 
    end 
    end 

任何幫助,將不勝感激。謝謝!

回答

0

我想通過延遲工作做到這一點。每次點更新或創建時,我都會關閉MapOverlayJob。

class MapsController < ApplicationController 

    def overlay 
    @points = Points.all 
    return render_to_string("overlay.kml") 
    end 

end 


class MapOverlayJob 

    def initialize 
    @s3_filename ||= "maps/overlay.kmz" 
    @zip_filename ||= "overlay.kml" 
    end 

    def perform 
    AWS::S3::S3Object.store(@s3_filename, 
          build_kmz_file, 
          S3_BUCKET, 
          :access => S3_ACL, 
          :content_type => Mime::KMZ) 
    end 

    private 
    def build_kmz_file 
     Zippy.new(@zip_filename => MapsController.new.overlay).data 
    end 

end