RABL: JSON objects without root key

I have this rabl template:

object @photo attributes :id child :comments do attributes :id, :body end 

What gives me this JSON answer:

 { photo: { id: 1, comments: [ { comment: { id: 1, body: 'some comment' } }, { comment: { id: 2, body: 'another comment' } } ] } } 

But I want it to look like this:

 { id: 1, comments: [ { id: 1, body: 'some comment' }, { id: 2, body: 'another comment' } ] } 

Why does rabl wrap each element of the array with an additional object called comment . Thus, when I access the collection in javascript, I have to write:

 var comment = image.comments[0].comment 

instead:

 var comment = image.comments[0] 

I know that if I include :comments in the list of attributes for the @photo object, it will work the way I want, but when I need another level of nested associations for each comment object, there is no way to handle this other than using child . but it gives me a JSON response that I don't want.

Perhaps I just misunderstand all this - can someone explain or help? Thanks!

+6
source share
1 answer

Got it!

Create a new file in config/initializers/rabl_config.rb :

 Rabl.configure do |config| config.include_json_root = false config.include_child_root = false end 
+7
source

Source: https://habr.com/ru/post/909024/


All Articles