fabrik42/acts_as_api

Json with all Root nodes

foton opened this issue · 4 comments

According to : https://github.com/fabrik42/acts_as_api/wiki/Differences-to-the-rails-3-default-json-response-structure
I setup ActsAsApi::Config.include_root_in_json_collections = true, but still getting

{
    "user": {
         "overdue_tasks": [
            {
                "id": 123,
                "content": "do something now"
            },
            {
                "id": 124,
                "content": "do something immediatelly"
            }
        ]
    }
}

instead of

{
    "user": {
         "overdue_tasks": [
            {"task" : {
                "id": 123,
                "content": "do something now"
            }},
            {"task" : {
                "id": 124,
                "content": "do something immediatelly"
            }}
        ]
    }
}

for

class User < ActiveRecord::Base
  has_many :tasks
  has_many :overdue_tasks, :class_name => "Task",  :conditions => { :due_date < Date.today }

  acts_as_api

  api_accessible :user_with_tasks do |t| 
     t.add :overdue_tasks, :template => :public
  end
end


 class Task < ActiveRecord::Base
    belongs_to :user

    acts_as_api

    api_accessible :public do |t| 
       t.add :id
       t.add :content
    end
  end

Somewhere in act_as_api/api_template.rb in

    def to_response_hash(model, fieldset = self, options = {})
        ...
    end

should be check for include_root_in_json_collections to add root element to each object in collection.
Particularly if object respond to acts_as_api .

Hi!

Seems like more people have this problem, there were several tickets about this topic.
Which Rails version are you using? 3/4?
It would be great if you would put together a small example app, so I can take a look.

I was using Rails 3. Now I am using Rabl to create JSON views on client.

Hi,
ActsAsApi::Config.include_root_in_json_collections = true is supposed to recreate the default response structure of Rails 3. This is (as stated in the article you linked) including a root node for first level collections but not for embedded collections. The parameter was meant to provide backwards compatibility for people moving from the this default style. Sorry if this confused you.

But I'm still glad that you got your problem solved. :)

Cheers,

Christian