Luc
Luc

Reputation: 17082

controller and restful application

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

Upvotes: 2

Views: 126

Answers (2)

djna
djna

Reputation: 55957

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.

Upvotes: 0

marcgg
marcgg

Reputation: 66535

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

Upvotes: 2

Related Questions