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?