Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails jbuilder remove header

I'm adding jbuilder to a Rails app -- great tool!

I'm getting the list of records I want, but it has extra output I don't want.

This is the jbuilder code:

json.locations @locations do |location|
 json.id location.id
 json.name location.name
end

The output is:

{
  - locations: [
    {
      id: 1,
      name: "Name 1"
    },
    {
      id: 2,
      name: "Name 2"
    },

What I need is:

[
    {
      id: 1,
      name: "Name 1"
    },
    {
      id: 2,
      name: "Name 2"
    },

How can I remove the { - locations:

???

Thanks!!

UPDATE:

I'm hoping there is a line of code for jbuilder that would exclude the root.

like image 922
Reddirt Avatar asked Feb 13 '13 15:02

Reddirt


1 Answers

Can you check if you have the following config?

ActiveRecord::Base.include_root_in_json = false

That should do the trick

Update

Try this instead:

json.array!(@locations) do |location|
 json.id location.id
 json.name location.name
end
like image 92
mathieugagne Avatar answered Sep 28 '22 01:09

mathieugagne