0
votes

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.

1
This is a shot in the dark, but have you tried @photos.first? - pdoherty926
@ethagnawl. Thanks, I just solved this :) - banditKing

1 Answers

0
votes

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"}]}