6

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.

Reddirt
  • 5,913
  • 9
  • 48
  • 126
  • I needed to exclude a root element when returning object `@foo = { bar: true, dry: false } json.foo @foo ` would print `{"foo": { "bar": true, "dry": false }}` and using a `merge!` function I succeeded `json.merge! @foo` gives output `{"bar": true, "dry": false}` as I wanted. – Ivan Bajalovic May 21 '15 at 13:28

1 Answers1

12

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
mathieugagne
  • 2,465
  • 1
  • 17
  • 18