How we have always done it
One thing that has always bothered me about the way we have always done it: JSON is becoming the new view in a world where server calls are becoming less frequent.
Typical rails controller
|1 2 3 4 5 6 7 8 9 10 11|| |
|1 2 3 4 5 6 7 8 9 10 11 12 13 14|| |
A new way
RABL (Ruby API Builder Language) provides a way to use a templating system for generating JSON or XML in your view. RABL is a simple DSL that allows you to define your JSON responses in an easy, clean way. It is easy to setup with Rails 2.X, Rails 3, and Rails 3.X.
Using RABL the above becomes:
New rails controller with RABL
|1 2 3 4 5 6 7 8|| |
New RABL view file
|1 2 3|| |
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18|| |
At the end of the day I think that RABL provides a better way to define my APIs. Please note that my examples above are overly simple. This was done to show the ideas I am talking about. Please check out RABL for yourself to see if you think it's a better solution.
Other RABL resources
RABL on GitHub - https://github.com/nesquena/rabl
'If you’re using to_json, you’re doing it wrong' - http://engineering.gomiso.com/2011/05/16/if-youre-using-to_json-youre-doing-it-wrong/