Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top