1

I have a before_filter which wants to default the response type depending on various aspects of the request and parameters. E.g.,

request.format = ( params.format ||= 'html' ) if ...    # an HTML-only request/client
request.format = ( params.format ||= 'json' ) if request.xhr?

the idea being that the respond_to do |format|; format.html { ... }; format.json { ... } would then render appropriately according to the client conditions. Some of the clients are coming in as type */* (presumably this is request.content_type ?) and I want to force these to be HTML responses. Doesn't seem to be working however. What is a clean way to do this, and without setting a default type for each route ? I.e. I just want to poke the response type into the request so that respond_to will switch on it accordingly.

tribalvibes
  • 2,097
  • 3
  • 25
  • 30

1 Answers1

1

request.format= should be an object of type MIME::Type.

So you'd have to do something like request.format = MIME::Types.type_for('html').first

Tal
  • 1,298
  • 2
  • 10
  • 20
  • thx, however this seems to have the "magic" side-effect of also changing `params.format`. without digging into the source, I just set it to a `StringEnquirer` so that e.g. `.html?` works but also `.json?` etc. – tribalvibes Jul 05 '12 at 07:23