ccocchi / rabl-rails

Rails 4.2+ templating system with JSON, XML and Plist support.
MIT License
209 stars 51 forks source link

rabl-rails renders JSON instead of XML in some cases #67

Closed monfresh closed 9 years ago

monfresh commented 9 years ago

Hi, I came across rabl-rails and wanted to use it because of the performance gains, but unfortunately, for some reason, it's not working in my Rails 4.1.8 app with Ruby 2.1.3, whereas the original rabl gem works. The only difference is the gems (and using collection @locations versus collection :@locations). When I used rabl-rails, the controller was rendering JSON instead of XML. Then when I replaced rabl-rails with rabl, everything was working fine.

I have no idea where to start looking to troubleshoot why rabl-rails doesn't work, but here is the source code: https://github.com/smcgov/ohana-api-smc

Here is the rabl template: https://github.com/smcgov/ohana-api-smc/blob/master/app/views/api/v1/locations/index.xml.rabl

Here is the controller: https://github.com/smcgov/ohana-api-smc/blob/master/app/controllers/api/v1/locations_controller.rb

Note that this template now has rabl-specific stuff in it, but you can still reproduce the issue with just a basic template that only defines attributes.

ccocchi commented 9 years ago

In your example app, the view params does not contain a :format key so rabl-rails falls back to the default JSON renderer. To avoid that, if no params are found, now we use the request format (similarly of what rabl gem is doing).

I fix this in d9514b9d2fcb0d53a47eb19ad9f5a013e1ec4bb3, let me know if that works, i'll release a new version if so.

monfresh commented 9 years ago

Thanks! I'll try it and let you know.

Since the rabl-rails gem is specific to Rails, I would have assumed that the format would be automatically picked up from the controller, as specified here. I've also specified the format in the rabl template name: index.xml.rabl.

Your README does not mention anything about specifying a format in the view params. Perhaps are you referring to this Content-Type issue? If so, that's because rabl support multiple frameworks, not just Rails. But since rabl-rails is specific to Rails, it should be able to automatically detect the proper format from the controller, no?

ccocchi commented 9 years ago

This have been fixed in v0.4.1