Topic: REST - how to put 2 ressources in the path

Hi everyone,
I have this problem, I m trying to create a Restful application, I have 2 different resources : Document and Schema, a document can have one schema and schema can be applied to different documents. The thing is i would like to allow the user to select a schema to use with a document with a rest path looking like this :

documents/2/schemas/3;link


but i have no idea how to perfome this. Should i create nested resources in routes.rb ? But i don't think it's a good solution cause i have specific methods to both ressources, for example

documents/1;manage or schemas/2;check

... as  you can see i really have no idea how to solve this problem, i just need some hints.

Thanks for your help

Re: REST - how to put 2 ressources in the path

So, there is only one schema that can be applied to a document, but a schema can have many documents, right?

If that is the case, I'd simply track the schema assigned to the document in the database. When you call a single document, you look up what it's schema is. There is no need for it to be present in the URL.

If you feel the need to have both present in the URL, make one a GET parameter rather than a method. Eg: document/2?schema=3;other_param=true;

You can generate this using the link_to method.

Last edited by KittyKate (2006-12-14 14:28:23)

Re: REST - how to put 2 ressources in the path

So, there is only one schema that can be applied to a document, but a schema can have many documents, right?

If that is the case, I'd simply track the schema assigned to the document in the database. When you call a single document, you look up what it's schema is. There is no need for it to be present in the URL.

Actually it is because the 2 resources are not related yet. Because you can also have no schema for a document. The goal of this method is to link them.

If you feel the need to have both present in the URL, make one a GET parameter rather than a method. Eg: document/2?schema=3;other_param=true;

You can generate this using the link_to method.

I don't think your method is very restful...

how would you define it in your routes.rb ?
what does your link_to looks like ? Something like this ?

<%= link_to 'link', link_document_path(document.id), :with => ... %>

Thanks for your help

Re: REST - how to put 2 ressources in the path

I haven't worked much with nested routes yet (unfortunately for me) but I'm pretty sure you can make this function without a lot of work.

You're looking to use the 'link' method on the SchemasController, right?  Then I think you set up the routes like this:

map.resources :documents do |documents|
  documents.resources :schemas => {:link => :get}
end

Or something like that.  this is 100% untried so if you work out the solution please post it so we can all see.

Good luck!

Re: REST - how to put 2 ressources in the path

OK, I solved it, you were right Danger, the thing was i also wanted to manipulated the schema alone, so I did this :

map.resources :views do |views|
    views.resources :schemas, :name_prefix => "view_",
                              :member => {
                                          :link => :get
                                          }
  end
 
  map.resources :schemas

The problem I had was that the method were overwritten by the second definition, but by adding the name_prefix it works fine.

Thanks for your help

P.S. :
The use is like this :

<%= schema_path(2) %> // for the common resource
<%= view_link_schema_path(3,2) %> // for the nested resource

Last edited by adamas (2007-01-03 04:37:27)

Re: REST - how to put 2 ressources in the path

Hey, thanks tons for posting that.  It's really great to see some real-world examples of nested routes.