🛤️ Ruby on Rails

Updated at 2016-01-06 22:44

__Keep the controllers skinny. __ They should only retrieve data for the view layer and shouldn't contain any business logic. All the business logic should reside in the model.

Controllers don't have to be tied to a single model. It's a very good practice to create controllers for complex model-to-model relationships.

# you have some Post and Comment models, posts/5/comments style
# don't try to stuff the logic in either of those controllers, create
# controller for the relation.

Use a lot of controllers. One of the most important ways to keep a Rails app in good shape is to create more controllers, even multiple controllers for same model or set of models. But always respect REST interface.

Limit method calls to one. Each controller action should invoke only one method other than an initial find or new.

Limit instance variables to two. Share no more than two instance variables between a controller and a view.

Immediately return after render.

# bad
render :text => "Howdy" and return

# good
render :text => "Howdy"

# bad
render :text => "Howdy" and return if foo.present?

# good
if foo.present?
  render :text => "Howdy"