How to customize JSON response from Rails app using Rabl gem

In my rails API app Im using the Rabl gem to render JSON. However, I ned the JSON to be in a specific format.

This is the JSON my rails app is sending as a response :

  [{"lat":"49.2505","lng":"-123.1119"},{"lat":"49.2515","lng":"-123.1109"}]

This is the response the client wants to get

  {"photos":[{"lat": 37.775, "lng": -122.4183333}, ...]}

Here is the code that renders the json in the file index.json.rabl

collection @photos
attributes :lat, :lng

I tried this code in the index.json.rabl file but the response came out as follows collection @photos node(:photos) { |photos|attributes :lat, :lng}

 [{"photos":["lat","lng"]},{"lat":"49.2515","lng":"-123.1109","photos":["lat","lng"]}]

Not exactly what the client needs

What would I have to change in this code to get the correct JSON response for the client.

Thanks a lot.

Answers


I solved this:

index.json.rabl

 collection @photos => :photos 
 attributes :lat, :lng

yields

 {"photos":[{"lat":"49.2505","lng":"-123.1119"},{"lat":"49.2515","lng":"-123.1109"}]}

Need Your Help

Cassandra connections keep increasing

cassandra nosql

My application's connection with cassandra keep increasing, MaxConnectionsPerHost doesn't work. i'm windering why?

How to load an XmlNode object ignoring undeclared namespaces?

c# .net xml xmlnode

I want to load up an XmlNode without getting an XmlException when an unrecognized namespace is present.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.