Rails jbuilder remove header

I am adding jbuilder to a Rails application - a great tool!

I get a list of entries that I want, but it has additional output that I don't want.

This is the jbuilder code:

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

Conclusion:

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

What I need:

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

How to remove {- locations:

???

Thank!!

UPDATE:

I hope there is a line of code for jbuilder that will eliminate the root.

+5
source share
1 answer

Can you check if you have the following configuration?

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
+11
source

All Articles