views:

56

answers:

2

Hello, I have a MVC web application (Ruby, Rack, Apache) and I want it to be restful. I have a dispatcher that will get incoming URI and call the appropriate controller. In my mind, a controller is there to handle every actions linked to a single model, I am wrong ? The thing I am not sure about is a case like the following:

If a user has several items, how can I get a specific item using the URI /user/user_id/item/item_id I mean, is this somethings that should be handled within the UserController or do I need to create another "cross classes" controller such as UserItemController that should take care of this king of stuff ? I hope I'm clear :-) Thanks a lot, Luc

+2  A: 

The organization of your methods in controllers or models have little to do with wether your app is going to be restful or not. Ideally try to map 1 controller to 1 model, but you could have a controller dedicated to an action (login for instance).

What you want to look at is the routes.rb file. This is where you will map ressources and so on.

  map.connect ':controller/:action/:id'

I recommend reading the excellent article of Ryan Tomayko about REST

marcgg
Great article, thanks
Luc
It has been very useful for me at the beginning. By the way, consider upvoting/accepting answers. Take a look at this faq post: http://meta.stackoverflow.com/questions/7237/how-does-reputation-work-on-stack-overflow/7238#7238 for more information about reputation and so on.
marcgg
(and welcome to SO!)
marcgg
A: 

How much code would there be in the controller? What dependencies would they have? How much would you need to rebuild if those depdencies change? Could you imagine subdividing responsbilities across a projecc team - some folks work on Users, other on Items?

My inclination is to keep the number of controllers small, so I would only separate them to enable parallel development or if you find that some part of the "URL space" seems to have a life of its own, lots of churn, while the rest is stable.

I think you can take a late decision on Controller granularity, refactoring if you find it's needed to increase granularity.

djna
- no much code- dependencies within controller ? Well, that's what i'm trying to figure out in fact- I guess the rebuilding work is not that important- No subdivising responsibility accross a project teamYes, you'r right, I think this decision can be taken later on, but in fact I wanted to know if there were some kinds of best practises in this case.- using a controller to query / update things within several different models... or- using a controller for a specified model but in this case how to handle cross model relation ships ?Thanks a lot
Luc