Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Individual root by template #8

Closed
medihack opened this issue Apr 15, 2011 · 5 comments
Closed

Individual root by template #8

medihack opened this issue Apr 15, 2011 · 5 comments
Assignees

Comments

@medihack
Copy link

Hello again, Christian.

Would it be possible to also add an option for setting roots by template?

E.g. when rendering something to JSON in acts_as_api with :root => :users then it results in something like this:

{ users: [ { name: "tina" }, { name: "eva" } ] }

it would be nice to render it easily this way (maybe by adding a :root option to :api_accessible):

{ users: [ { user: { name: "tina" } },  { user: { name: "tina" } } ] }

The last one is also the default output of render :json => users.

@ghost ghost assigned fabrik42 Apr 18, 2011
@fabrik42
Copy link
Owner

Hi Kai,

I see your problem and I will take care of it this week. This is also a feature I want, so I can make acts_as_api compatible with backbone.js out of the box. :)

@fabrik42
Copy link
Owner

Btw: I've chosen another default response format as the Rails 3 default response format is imho not consistent, when it comes to render sub resources:

While rails wraps a collection of models in a root node by default

{ users: [ { user: { name: "tina" } },  { user: { name: "tina" } } ] }

It doesn't do this for included associations, e.g.

[
    {
        "user": {
            "created_at": "2011-04-26T08: 43: 10Z",
            "updated_at": "2011-04-26T08: 43: 10Z",
            "id": 1,
            "tasks": [
                {
                    "created_at": "2011-04-26T08: 44: 22Z",
                    "completed": false,
                    "updated_at": "2011-04-26T08: 44: 22Z",
                    "id": 2,
                    "user_id": 1,
                    "description": "Be a Jedi"
                },
                {
                    "created_at": "2011-04-26T08: 44: 30Z",
                    "completed": false,
                    "updated_at": "2011-04-26T08: 44: 30Z",
                    "id": 3,
                    "user_id": 1,
                    "description": "Destroy Deathstar"
                }
                ...
            ],
            "last_name": "Skywalker",
            "first_name": "Luke"
        }
    },
    ...
]

But I will add a config option to restore the default Rails behaviour. :)

@fabrik42
Copy link
Owner

Hi Kai,

just add

ActsAsApi::Config.include_root_in_json_collections = true

in your initializers and the response should look like the Rails 3 response.

@medihack
Copy link
Author

Hi Christian, you are right with those inconsistencies in the Rails output. That's why we decided to use the more consistent way of your ActsAsApi default representation in upcoming projects. Thanks again for the great support.

@fabrik42
Copy link
Owner

You're welcome! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants