1.10. Action Controllers

Action controllers or controllers are the entry points of a web request. They are made up of one or more actions that are executed on requests and then either render a template or redirect to another action. An action is defined as a public method on the controller, which will automatically be made accessible to the web server through Rails routes. For more on Action controllers, go to http://api.rubyonrails.org/classes/ActionController/Base.html.

Here is my MainController from my ClearPlay Movie List iPhone webapp (http://clearplaymovies.mobi):

class MainController < ApplicationController
  
  def index
  end
  
  def movies
    @letter = params[:id]
    if @letter == "other"
      criteria = []
      ("A".."Z").each do |letter|
        criteria << "title not like '#{letter}%'"
      end
      @movies = Movie.find(:all, :conditions => criteria.join(' and '), 
       :order => :title)
    else
      @movies = Movie.find(:all, :conditions => "title like '#{@letter}%'", 
       :order => :title)
    end
  end
  
end

By default, actions render a template in the app/views directory that corresponds to the name of the controller and action after executing code in the action. For example, the index action of the MainController here would render the template app/views/movies/index.html.erb.

1.10.1. Requests

Requests are processed by the ActionController framework by extracting the value of the action key in the request parameters. This value should hold the name of the action to be performed. Once the action has been identified, the remaining request ...

Get Rails Pocket Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.