0

i have the following jbuilder class:

json.array!(@users) do |user|
  json.user_id user.id
  json.user_name user.full_name
  ...
end

But i want all @users data inside a :data attribute. And a code and a message for the response. Exactly like this:

{ :code => 200, :message => "Succesful request", :data => {[{user_id: 12, user_name: "Jason"},{user_id: 13, user_name: "Peter"}...]} }

How can i do it?

Thanks in advance.

ntonnelier
  • 1,539
  • 3
  • 23
  • 49

1 Answers1

0

You should't use array! method. Maybe you can do them as below:

json.code    response.status
json.message 'you want to return message here'
json.data @users do |user|
  json.user_id user.id
  json.user_name user.full_name
  ...
end
shoji
  • 490
  • 2
  • 10
  • thanks shoji! This also works: json.data do json.array! @users do |user| json.user_id user.id json.user_name user.full_name ... end end – ntonnelier Feb 20 '15 at 13:28