views:

65

answers:

1

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?

+4  A: 

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
antage
Props for reminding me about the extension aliases, however this doesn't work as rails doesn't recognise the route. I'll play around with the `routes.rb` a bit to see if that gets it going.
bjeanes
Changing the routes to be something like this (at least, in Rails 3) did the trick: `resources :people, :constraints => { :format => /[a-z]+(\.[a-z]+)?/ }`
bjeanes