views:

224

answers:

3

Hi,

I want to be able to render both html and xml. By default html is rendered unless we add a format e.g. /myresource.xml. I want to render xml by default and only respond to .html format:

GET /myresource/ returns html

GET /myresource.xml returns xml

I would like:

GET /myresource/ returns xml

GET /myresource.html returns html

Is there an easy way to achieve this?

Regards,

Chris

A: 

try adding :format => 'xml' to your resource definition, though i'm not sure restfull stuff should work this way

Eimantas
+1  A: 

Since this nonstandard in Rails, it might best be solved via mod_rewrite in Apache. Map /whatever to /whatever.xml and map /whatever.html to /whatever.

Sarah Mei
+1  A: 

If you want to control this at the controller level you can do the following (either in the ApplicationController or the MyResourceController)

class MyResourceController (or ApplicationController) < ApplicationController
  before_filter :change_format

  ...

  protected
  def change_format
    params[:format] = 'xml' if params[:format].blank?
  end 
end

adnan.