Controller best practices

I’m frequently building controllers where i would like multiple methods
(in addition to index, edit, show, etc.). Most of the time the actions i
desire could be lumped into show as they are simple GET operations,
however I don’t want to put too much logic in any one controller action.

Here is a quick example of two different ways to achieve the same
thing…

class TwitterFriendController < ApplicationController

lump everything into show?

def show
if params[:id] == “follow”
users = current_user.following
elsif params[:id] == “follow_me”
users = current_user.users_who_follow_me
elsif params[:id] == “following_follow_me”
users = current_user.following_who_follow_me
elsif params[:id] == “following_who_do_not_follow_me”
users = current_user.following_who_do_not_follow_me

end
respond_with do |format|
format.json do {…}
end
end

or split everything out into separate methods, this requires

additional routing
def following

end

def users_who_follow_me

end

def following_who_follow_me

end

def following_who_do_not_follow_me

end
end

Everything in show

  • a ton of logic in one method
  • DRY ? # lots of extra code needed for logic
  • Less routing

Seperate Methods

  • More routing
  • not DRY
  • Easy method lookup
  • Easier to read individual methods

So again the real question is, which one of those techniques are less
bad.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs