README.rdoc

Action Pack – From request to response

Action Pack is a framework for handling and responding to web requests. It
provides mechanisms for routing (mapping request URLs to actions),
defining controllers that implement actions, and generating
responses by rendering views, which are templates of various
formats. In short, Action Pack provides the view and controller layers in
the MVC paradigm.

It consists of several modules:

Action Dispatch, which parses information about the web request, handles
routing as defined by the user, and does advanced processing related to
HTTP such as MIME-type negotiation, decoding parameters in POST/PUT bodies,
handling HTTP caching logic, cookies and sessions.

Action Controller, which provides a base controller class that can be
subclassed to implement filters and actions to handle requests. The result
of an action is typically content generated from views.

With the Ruby on Rails framework, users only directly interface with the
Action Controller module. Necessary Action Dispatch functionality is
activated by default and Action View rendering is implicitly triggered by
Action Controller. However, these modules are designed to function on their
own and can be used outside of Rails.

class WeblogController < ActionController::Base
# filters as methods
before_filter :authenticate, :cache, :audit
# filter as a proc
after_filter { |c| c.response.body = Gzip::compress(c.response.body) }
# class filter
after_filter LocalizeFilter
def index
# Before this action is run, the user will be authenticated, the cache
# will be examined to see if a valid copy of the results already
# exists, and the action will be logged for auditing.
# After this action has run, the output will first be localized then
# compressed to minimize bandwidth usage
end
private
def authenticate
# Implement the filter with full access to both request and response
end
end

Started GET "/weblog" for 127.0.0.1 at Fri May 28 00:41:55
Processing by WeblogController#index as HTML
Rendered weblog/index.html.erb within layouts/application (25.7ms)
Completed 200 OK in 29.3ms
If Active Record is used as the model, you'll have the database debugging
as well:
Started POST "/posts" for 127.0.0.1 at Sat Jun 19 14:04:23
Processing by PostsController#create as HTML
Parameters: {"post"=>{"title"=>"this is good"}}
SQL (0.6ms) INSERT INTO posts (title) VALUES('this is good')
Redirected to http://example.com/posts/5
Completed 302 Found in 221ms (Views: 215ms | ActiveRecord: 0.6ms)
You specify a logger through a class method, such as:
ActionController::Base.logger = Logger.new("Application Log")
ActionController::Base.logger = Log4r::Logger.new("Application Log")

Caching at three levels of granularity (page, action, fragment)

class WeblogController < ActionController::Base
caches_page :show
caches_action :account
def show
# the output of the method will be cached as
# ActionController::Base.page_cache_directory + "/weblog/show/n.html"
# and the web server will pick it up without even hitting Rails
end
def account
# the output of the method will be cached in the fragment store
# but Rails is hit to retrieve it, so filters are run
end
def update
List.update(params[:list][:id], params[:list])
expire_page :action => "show", :id => params[:list][:id]
expire_action :action => "account"
redirect_to :action => "show", :id => params[:list][:id]
end
end

All exceptions raised on actions performed on the request of a local user
will be presented with a tailored debugging screen that includes exception
message, stack trace, request parameters, session contents, and the
half-finished response.

This simple setup will list all the posts in the system on the index page,
which is called by accessing /weblog/. It uses the form builder for the
Active Record model to make the new screen, which in turn hands everything
over to the create action (that's the default target for the form
builder when given a new model). After creating the post, it'll
redirect to the show page using an URL such as /weblog/5 (where 5 is the id
of the post).