Jseb
Jseb

Reputation: 1934

Converting my json index

I am trying to convert the following json index from the following style

[{"event":{"title":"test"}},{"event":{"title":"test2"}}]

but instead of having it this way i am wondering if its possible to create the index json action as the following json structure

{"event": [ { "title":"test"},{ "title":"test2" }] }

I can generate the first style by the following code

respond_with @events.as_json(:root => true, :only => [:title])

But not sure how to have rails to generate json index structure has the second format

Upvotes: 0

Views: 86

Answers (1)

ouranos
ouranos

Reputation: 1234

I think you'll have to build the json yourself, ie:

render :json, {event: @events.map {|e| {title: e.title}}}.to_json

If it gets too complex, you probably should use RABL (see the railscast here)

Upvotes: 2

Related Questions