Skinny controllers are not enough

Written in Development by Oriol Gual — March 02, 2011

Starve them to death

At the end of the day, what do you really want? Write less code. Less code means fewer bugs and better mantainability, among many other things. Nothing new. Something else everyone should be doing (if not already) is to keep fat models and skinny controllers, but keeping your controllers skinny doesn't mean you are not repating yourself. Thus, skinny controllers are not enough.

Enter Inherited Resources (corresponding Railscast). In an ideal world, a typical RESTful post controller would become something like this:

# Inherited Resources controller
class PostsController < InheritedResources::Base
end

This is controllers on diet. In the long run you'll probably need to change something and add some code, but it's definitely very hepful. If you're using Inherited Resources you should also look at Responders documentation and then tweak you locales accordingly.

I've always been a fan of Inherited Resources, but recently (thanks to this tweet by @pat) I discovered Decent Exposure, and I really like it. Is a shift from what Inherited Resources does, tackling the problem with a different solution:

# Decent Exposure controller
class PostsController < ApplicationController
  expose(:post)

  def create
    post.save
    respond_with(post)
  end

  def update
    post.update_attributes(params[:post])
    respond_with(post)
  end

  def destroy
    post.destroy
    respond_with(post)
  end
end

It is not as DRY as Inherited Resources, but maybe this is a good thing: there's no hidden voodoo that magically does everything. It's not that Inherited Resources does something wrong, but sometimes you want more control on what is happening in your application or, due to the lack of knowledge, you're doing something wrong with it but you fail to realize it was due to all the magic happening behind the scenes.

I'd really like to share some experiences about both Inherited Resources and Decent Exposure? Which one do you prefer? What has been your experience so far? Has anyone tried, if it makes sense, to use Decent Exposure together with Responders?

View all posts tagged as