Topic: Controller Code Tutorial

Hi.  Most tutorials do a bang-up job teaching WHAT a controller is, and WHAT an action is in relation to the rest of a rails project.  But I have yet to read a tutorial that does a good job explaining HOW the code in a controller actually works. 

Specifically:

class UsersController < ApplicationController

   def action
      * THIS PART HERE! *
   end 

Obviously, it is Ruby code.  But learning the Ruby code is too much of a "bottom-up" approach to learning these things.  It seems like there should be better "top-down" methods of learning about such code.  So, here are some questions that, if answered by RoR geniuses, might help a great deal:

(1) What are the 10 most common code snippets you use in the controller (what do they do and how do they work)?

(2) How do you know which way data is flowing?  (From the model to the view, from a form to a model, etc..)

(3) What are common misconceptions about code in controllers?

(4) What are the most common mistakes people make when writing code in controllers?

Thank You.

Last edited by Starflyer59 (2011-02-28 02:20:35)

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent

Re: Controller Code Tutorial

1) What are the 10 most common code snippets you use in the controller (what do they do and how do they work)?
a) Model.find(params[:id])
- this queries the "model" (the object the controller represents) for the object with an id of params[:id].
params[:id] is passed in via the request, usually in the form /users/<id>
b) Model.new
- instantiates a new object of Model, all attributes will be nil
c) Model.new(params[:model])
- instantiates a new object of Model with provided attributes.
params[:model] is generally full of POST data from the form of a new action.
d) @model.update_attributes(params[:model])
- updates a model object with the provided data in params[:model]
params[:model] is full of PUT data from the form of an edit action
e) @model.destroy
- calls the "destroy" method for the given model instance, by default this results in a SQL DELETE
f) @model.save
- Saves the instantiated object to the DB (or some persisted form)
g) respond_to do|format| block
- figure out what format the response to the request should be in, xml, js, html, etc. and express what we should do for the requested format
h) render :update do |page| block
- this is rjs ... updating the current DOM with actions in this block
i) before_filter
- something to do before an action is called, I use this to build variables to be used for my selects or for authentication.
j) if/else/end block
- you'll see this a lot, standard if then else.

2) How do you know which way data is flowing?  (From the model to the view, from a form to a model, etc..)
All "requests" are made to the controller.  The controller will query the model if needed to get what it needs to present the view.
So user talks to the controller and the controller talks to the model, the model responds to the controller the controller then passes that data to the view and the view is presented to the user as the "response".

3) What are common misconceptions about code in controllers?
I like to keep the controllers as close to per CRUD (scaffolded resource) as possible.  You do not want to do a whole lot in the controller or views, save all the logic for the models.  Focus on building your forms properly so that params[:model] contains all that you need and you don't wind up doing @model.new(params[:model]); @model.x = params[:x] ... etc., that should be taken care of in the params as params[:model][:x].  I hardly touch my controllers, and if I do it's to add before_filters and maybe private methods to be called by the before_filters.

4) What are the most common mistakes people make when writing code in controllers?
The mistake is to write too much code in the controllers, follow the standard of light controller and heavy models.

Re: Controller Code Tutorial

Awesome.  Digesting.......

I'm so abstract that automatic doors at grocery stores do not even open for me. - @thatdankent