REST - Implementing HABTM in the real world

Hi again - another REST question!

I think I've got a good grasp on it all now, except for how to implement HABTM properly. I understand you need turn the join model into a controller, such as 'memberships' below, and then use CRUD on that to perform joining actions.

class Group < ActiveRecord::Base

  has_many :memberships   has_many :users, :through => :memberships

class User < ActiveRecord::Base

  has_many :memberships   has_many :groups, :through => :memberships

class Membership < ActiveRecord::Base

  belongs_to :user   belongs_to :group

What I don't understand is how that actually works in your code. Let's say I'm looking at all of the groups (GET /groups), and want to get the current user to join one. Presumably, I'd POST to /memberships? group_id=1&user_id=1.

But how would I handle if that failed, or they didn't have permission to do so? And how would I handle the controller returning to the page it came from? Because I could hit the same action from both sides of the HABTM relationship. (User joining a group, and group selecting a user).

Are there any examples of this out there?

Thanks a lot!

Tom