1

By default, Angular's $resource.$save method sends a request with parameters in the "base" scope. For example, creating resource with properties {name: 'John Smith', email: '[email protected]'} will result in a POST with parameters:

name=John Smith&[email protected]. 

However, I have a Rails controller that needs the resource scoped to a sub-hash (more like the normal railsy way of doing things). I would want that same create request to have parameters:

user[name]=John Smith&user[email][email protected]

The problem is, I just don't see a way to do this using $resource. Is it possible?

1 Answer 1

1

This can be accomplished by using $resource's transformRequest option. Below is an example using the update action (written in coffeescript):

update:
  method: 'PUT'
  transformRequest: (data, headersGetter) ->
    data = _.omit(data, '$promise', '$resolved')
    json = {'user': data}
    return JSON.stringify(json)

If you want to do this by default, I recommend writing a service that wraps $resource and adds the transformRequest functions to all actions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.