The magic that is ActionController

We use controllers a lot in our applications but do we really understand the magic that is behind them?

Controllers are considered to be the middleman between the model and the view. Most inherit from ApplicationController. But did you know that ApplicationController inherits from another controller called ActionController::Base?

By using inheritance, (that’s the < ApplicationController bit) we gain a whole lot more functionality that we can use in our app.

ActionController::Base is part of the Rails library Action Pack. It is the base class for all controllers in a Rails application. It provides many helper methods including the filtering of inbound http requests, access to our model, and rendering a view. But there are many more, see the rails API for more info.

Below is a simple example of how it might look. It looks like it is doing very little. In fact, it is doing a lot! It is getting the params from the url, asking our model for that record (using the id) and then rendering the show.html.erb view.

class IdeasController < ApplicationController
  def show
    @idea = Idea.find(params[:id])

Easy huh?

173 thoughts on “ActionController

  1. instagram takipçi satın al says:

    Hmm it seems like your blog ate my first comment (it was super long) so I guess I’ll just sum it up what
    I wrote and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to
    the whole thing. Do you have any tips for newbie blog writers?
    I’d definitely appreciate it.

  2. antalya haberleri says:

    Having read this I thought it was extremely informative. I
    appreciate you spending some time and effort to put this informative article together.
    I once again find myself personally spending way too much time
    both reading and commenting. But so what, it was still
    worth it!

Leave a Reply

Your email address will not be published. Required fields are marked *