nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.65k stars 335 forks source link

Reuse templates for diferent formats [:json, :xml, :plist ...] #267

Closed NicosKaralis closed 12 years ago

NicosKaralis commented 12 years ago

I'm having some difficulties in integrating two or more formats using the same template.

I have the following views:

cities.rabl
city.rabl

in my controller i have:

respond_to :json, :xml, :plist

if I try access: /city.json or /city.xml or /city.plist works fine

but if I try to access /cities.json or /cities.xml or /cities.plist returns me this error:

Cannot find rabl template '/api/cidade' within registered ([]) view paths!

if i change the city.rabl to city.json.rabl then /cities.json or /cities.xml or /cities.plist begin to work, but /city.xml or /city.plist stop working

the basic code for city and cities are the following:

api/city.rabl

object @city
attributes :id, :name

api/cities.rabl

collection @cities
extends "api/city"

my question is: Is there a way to create a "generic" view witch i can use the same to many different formats?

databyte commented 12 years ago

I think this is less of a RABL issue or more of a Rails template lookup scenario.

My initial concern is with your application responding with cidade as the next lookup. Do you have any ActiveSupport inflections setup to rename the plural of city to cidade? (local differences maybe)

Overall city.rabl and cities.rabl should work. Another option if you're doing it in response to a single city and many cities is taking the controller route of INDEX and SHOW. In my case, I would suggest that your cities controller has a show method that renders cities/show.rabl and an index method that renders cities/index.rabl which would have been city.rabl and cities.rabl respectively.

You may have better luck opening up the question to StackOverflow. Link the question here and I'll help there as well.

databyte commented 12 years ago

If you have any other questions, just reopen.