views:

108

answers:

3

So I am in a situation where I have to decide whether or not to have a separate controller for a particular piece of code. We have a home page that acts like a hub for the rest of the site. The page is accessible to all users (logged in as well as non-logged-in). I was thinking about having home as a separate controller and an action called index. Thinking about this situation, I started wondering if there are any rules or guidelines on this front.

My perception has been that if a code revolves around an entity, separation is needed. (Similar to REST guidelines) If the entity is a noun, it should be a controller. If the entity is a verb, it should probably be an action and should reside in the controller whose name is the same as that of the noun that the verb refers to. Some colleagues suggested that since this is one action, it should reside in the some existing controller and should be named home. I strongly disagreed, however, I could not find a trusted source that would back me up on this.

Would like to know your thoughts.

A: 

In this case I have to agree with your co-workers.

REST is a nice approach to take when dealing with resources, as you say. This allows you to create a consistent interface especially with a view to creating a web service.

However REST doesn't actually map too well to a web browser setting. You'll notice for example that even for resources your /edit and /new actions are just GET requests returning an HTML form pointing to the relevant RESTful action. 'edit' and 'new' aren't RESTy at all.

Similarly, the home page is generally a user-friendly amalgamation of various data, not suited to a RESTful interface. So either just stick an extra controller in with one action, or alternatively use an existing controller's 'list' action as the home page

Gareth
"However REST doesn't actually map too well to a web browser setting."Wow, that's going to be a shock to Roy Fielding, the guy who came up with the name REST. He extracted the definition of REST based on the way successful web applications do work. The fact that ROR created these /edit and /new endpoints is a reflection of Rails misuse of REST not a lack of applicability.
Darrel Miller
Well, that did not answer the question about when should one separate out code into a new controller. I see controllers as contextual objects and methods as their behavior. The question still remains that given a piece of code, how do I know that I need a new controller for this code or should this just be an action or set of actions in an existing controller. If one could give a solution along with reasons as to what to do in case of the `home` page (As given in the question) situation, this question would get answered automatically.
Chirantan
@Darrel - I'm not saying that REST doesn't work on the web, it's a useful paradigm for HTTP services. But the way there's no reflection on the service without adding extra HTML forms is a problem when using it in web apps. Also, browsers only typically support the GET and POST verbs, with more workarounds needed to allow the other verbs to be used.
Gareth
A: 

The problem starts with the phrase

If an entity is a verb

If you are attempting to produce a RESTful architecture, an entity cannot be a verb. The only verbs allowed if you are using HTTP are GET, PUT, POST, DELETE, HEAD, OPTIONS. All entities should be mapped to some noun and if you are trying to retrieve that entity you should be using the verb GET. Personally, I would map that to the method Get() on my controller but I don't know if Rails lets you do that.

Darrel Miller
The remarks directed at Gareth's response should be in the comments section, not the Answer.
Sailing Judo
@Sailing Judo Happy?
Darrel Miller
A: 

The quick (and unhelpful) answer is that either way works fine.

I think that everybody comes across this decision at one point, and the decision you make depends on the likely future of the website... which means it's prone to premature optimisation... but that's always the catch,

As you've probably guessed by now, "home" is in some ways a verb as well as a noun, thus why you're having trouble figuring out what to do.

The answer depends on a combination of interpretation of your website structure and how much time is available to you...

if you have very little time to work on this... then stuffing the 'home' action into another controller is often considered the expedient option. It works, it lets you move onto other (probably more productive) tasks.

However, I agree that sometimes it is good to step back and think about what you're doing and whether it could be done "better"... in this case, though it's harder to define "better" - as it's unlikely that putting the home action in a new controller would be measurably faster... and if it's the only action int he controller... it's debatable whether it's better, architecturally, to just adding it onto an existing controller...

So we start in on what is mostly a philosophical debate... in other words, no answer will be "more correct" than the other- it's more a matter of taste and circumstance. In this case, the debate hinges on making the structure more RESTful.

To be faithful to RESTful architecture, you would indeed move the action into it's own controller... but you'd first have to identify what the entity is. The "home" page is often not readily identifiable as a specific db entity... it's more often a portal-page.

Sometimes you can pick an entity eg online shops will often have a home page that is actually a "products#index" variation, or sometimes the "home" page is a UserAccount#show page... but more often, your home page will not be simple, and will combine information from multiple entities... thus why it is difficult to decide what the "right" architecture will be.

If you cannot identify a specific entity, then there is a valid debate as to whether to move the action into a specific controller.

however, you can always create a new "entity" that is centred around the architecture of the site. This is especially likely if you are going to come up with other non-entity-specific pages for the site (eg T&Cs or an "about our company" page).

The usual fallback being a "PageController" (or similar name) which isn't linked to an Active Record model, but to a more nebulous entity, in this case a "page" which is recognisable to a user of the website (eg the "home page" and the "T&C page" and the "about page"). Each action would be for a specific page...

So, it's up to you as to whether this fits better with your view of the architecture of your system... and whether it's worth the effort... but that's my view on the debate. :)

Taryn East