Hello,
Sorry if this has been answered in old posts, I couldn’t find anything.
This has been killing me all the morning
I’m a bit new to REST, and
I’m trying to model my app routes ‘restfully’, but I get lost on many
things.
I’ve been searching how to do this with resources:
I have a Service model that has many Sections, and Sections have and
belong to many Contents. But the Contents don’t have to be in a Section
(it’s optional).
I want to be able to add a content to a section, and remove a content
from a section. With nested resources I could do something like this:
map.resources :services, :shallow=>“true” do |service|
service.resources :sections do |section|
section.resources :contents
end
end
map.resources :contents
But it isn’t what I’m searching. I want to do something that allows me
to have a route like “POST /services/32/sections/14/add_content/5”,
because “POST /services/32/sections/14/contents/” would create a content
in the section 14, but I don’t want a new one. I only want to add an
existing content to a existing section (or delete it). I saw somewhere a
forum post proposing something like (adapted to this case):
map.resources :services, :shallow=>“true” do |service|
service.resources :sections, :member=>{:add_content=>:post,
:remove_content=>:delete} do |section|
end
end
map.resources :contents
But the “member” functions only allow me to operate on the section, and
I cant specify the content id (only by giving a ?content_id=5 after the
URL). Is there anything I can do without falling back to map.connect?
Best regards and thanks in advance,
Diego.