# RESTful routes and adding/removing associations

**URL:** <https://www.ruby-forum.com/t/restful-routes-and-adding-removing-associations/144406>\
**Category:** Rails\
**Created:** [July 29, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/restful-routes-and-adding-removing-associations/144406 "2008-07-29T12:39:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gudleik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gudleik](https://www.ruby-forum.com/u/gudleik)\
**Post date:** [July 29, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/restful-routes-and-adding-removing-associations/144406/1 "2008-07-29T12:39:55Z")

</div>

Hi,  
I’m trying to make a restful route to Add and Remove relationships  
between a Contact and a Customer.

I have a Contact and a Customer model that looks like  
def Contact \< ActiveRecord::Base  
has\_many :customer\_mappings, :class\_name =\>  
“ContactMapping”, :foreign\_key =\> “contact\_id”  
has\_many :customers, :through =\> :customer\_mappings  
…  
end

Customer model looks similar but with has\_many :contacts… instead.

In my contacts and customers controller I have methods to add and  
remove a contact/customer, like this (very simplified):  
ContactsController#add\_customer  
Contact.find(params[:id]).customer\_mappings \<\<  
ContactMapping.new(:customer =\> Customer.find(params[:customer\_id])  
end

Now, what is the best way to accomplish a named route for this in Rest-  
style ?  
I already have the map.resources :contacts in routes.rb.

What I’d like is an add\_customer\_contact\_url (or similar) method so I  
could do something like

form\_remote\_tag(:url =\> add\_customer\_contact\_url(@contact), :method  
=\> :post) and then just pass along the customer\_id.

I’ve tried various settings in routes.rb but I can’t seem to get it  
right.

Can anyone help?

---

<div class="post-metadata">

**Author:** ![gudleik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gudleik](https://www.ruby-forum.com/u/gudleik)\
**Post date:** [July 29, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/restful-routes-and-adding-removing-associations/144406/2 "2008-07-29T12:54:34Z")

</div>

When i put this in routes.rb  
map.resources :contacts, :member =\> { :add\_customer  
=\> :post, :remove\_customer =\> :delete }

the add\_customer\_contact\_url(@contact) is working as expected.

But the route for remove\_customer\_contact\_url doesn’t work, or it  
“works” but not as expected.

‘rake routes’ says:  
remove\_customer\_contact DELETE /contacts/:id/remove\_customer  
{:action=\>“remove\_customer”, :controller=\>“contacts”}

But i expected an URL like /contacts/:id/remove\_customer/:customer\_id

Any ideas?

---

<div class="post-metadata">

**Author:** ![gudleik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gudleik](https://www.ruby-forum.com/u/gudleik)\
**Post date:** [July 29, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/restful-routes-and-adding-removing-associations/144406/3 "2008-07-29T15:24:03Z")

</div>

Ok, I had to buy the REST for Rails 2 screencast from PeepCode  
[https://peepcode.com/products/rest-for-rails-2](https://peepcode.com/products/rest-for-rails-2), and after watching  
it for 5minutes I got the answer 🙂

In routes.rb i have:  
map.resources :contacts, :member =\> { :add\_customer  
=\> :post, :remove\_customer =\> :delete }

and in my view I can now call  
\<%= link\_to “Remove”,  
remove\_customer\_contact\_url(@contact, :customer\_id =\>  
customer\_id), :method =\> :delete %\>

Well worth $9 🙂
