Search code examples
ruby-on-railsmime-typesrespond-to

rails controller respond_to format with two extensions (e.g. tar.gz)


Is there a mechanism or accepted approach for responding to requests that have a more complicated format extension?

My specific scenario involves returning a plist file. However, I need to sometimes return this file as an XML plist file and sometimes as a binary plist file.

I thought that URLs composed like /resources.xml.plist and /resources.binary.plist would be a nice way to distinguish between them. I'd then need to add a MIME type for binary.plist and one for xml.plist and then somehow respond_to these formats.

Does any one know how this might be accomplished and/or have ideas for a nicer approach?


Solution

  • Take a look at tutorial "Using custom mime types".

    
    Mime::Type.register "application/xml", :plist_xml, [], ["xml.plist"]
    Mime::Type.register "application/octet-stream", :plist_binary, [], ["binary.plist"]
    
    ...
    
    respond_to do |format|
      format.plist_xml { ... }
      format.plist_binary { ... }
    end