0

I have been working on a web app using a Rails API and AngularJS. As models I have Teams and Users. You can add existing users to your team at anytime.

Since I want to add users to a team from AngularJS I wonder which route / controller should handle this action. (Adding an existing users to team.users)

Would this be a PUT api/teams/:id/users or PUT api/teams/:id or even a POST to api/teams/:id/:users

Keep in mind I am not creating a new user here but appending an existing one to a team's users.

Thank you!

3
  • Use POST to api/teams/:id/:users more info stackoverflow.com/questions/630453/put-vs-post-in-rest Commented Feb 3, 2015 at 15:32
  • Even though I am not creating a user ? Commented Feb 3, 2015 at 15:33
  • Yes as you are adding a user to a team. If you are creating a user you would post to api/users Commented Feb 4, 2015 at 11:00

1 Answer 1

2

None of the above; I would POST to /memberships.

You're not creating or updating a user, and you're not creating or updating a team.

You're creating a new joining record for users and teams. I would call that type of record a Membership, and provide an API for managing them. When a user joins a team, it's a POST to /memberships. When a user leaves a team, it's a DELETE to /memberships/:id.

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

2 Comments

Makes a lot of sense. Would that require me to remove the has_and_belongs_to_many relationships I had and replace them with has_many through: ?
Yes, you shouldn't use has_and_belongs_to_many any ways. It's more or less out of favour at this point.

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.