2010-07-19 07:51:04 -04:00
|
|
|
= Action Pack -- From request to response
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-08-03 15:12:16 -04:00
|
|
|
Action Pack is a framework for handling and responding to web requests. It
|
2010-07-19 07:51:04 -04:00
|
|
|
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.
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
It consists of several modules:
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
* 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.
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
* 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.
|
|
|
|
|
|
|
|
* Action View, which handles view template lookup and rendering, and provides
|
|
|
|
view helpers that assist when building HTML forms, Atom feeds and more.
|
2011-04-03 03:59:37 -04:00
|
|
|
Template formats that Action View handles are ERB (embedded Ruby, typically
|
2011-03-29 16:21:21 -04:00
|
|
|
used to inline short Ruby snippets inside HTML), and XML Builder.
|
2010-07-19 07:51:04 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
A short rundown of some of the major features:
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
* Actions grouped in controller as methods instead of separate command objects
|
2008-07-16 08:00:36 -04:00
|
|
|
and can therefore share helper methods
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2011-05-17 08:22:40 -04:00
|
|
|
class CustomersController < ActionController::Base
|
2006-09-03 14:34:28 -04:00
|
|
|
def show
|
2004-11-23 20:04:44 -05:00
|
|
|
@customer = find_customer
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def update
|
|
|
|
@customer = find_customer
|
2010-07-19 07:51:04 -04:00
|
|
|
if @customer.update_attributes(params[:customer])
|
|
|
|
redirect_to :action => "show"
|
|
|
|
else
|
|
|
|
render :action => "edit"
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
2010-07-19 07:51:04 -04:00
|
|
|
def find_customer
|
|
|
|
Customer.find params[:id]
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
{Learn more}[link:classes/ActionController/Base.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2011-04-03 03:59:37 -04:00
|
|
|
* ERB templates (static content mixed with dynamic output from ruby)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2011-05-18 23:29:57 -04:00
|
|
|
<% @posts.each do |post| %>
|
2004-11-23 20:04:44 -05:00
|
|
|
Title: <%= post.title %>
|
|
|
|
<% end %>
|
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
All post titles: <%= @posts.collect{ |p| p.title }.join(", ") %>
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
<% unless @person.is_client? %>
|
|
|
|
Not for clients to see...
|
|
|
|
<% end %>
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
{Learn more}[link:classes/ActionView.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
* "Builder" templates (great for XML content, like RSS)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
xml.rss("version" => "2.0") do
|
|
|
|
xml.channel do
|
|
|
|
xml.title(@feed_title)
|
|
|
|
xml.link(@url)
|
|
|
|
xml.description "Basecamp: Recent items"
|
|
|
|
xml.language "en-us"
|
|
|
|
xml.ttl "40"
|
|
|
|
|
2011-05-18 23:29:57 -04:00
|
|
|
@recent_items.each do |item|
|
2005-02-23 20:29:43 -05:00
|
|
|
xml.item do
|
|
|
|
xml.title(item_title(item))
|
|
|
|
xml.description(item_description(item))
|
|
|
|
xml.pubDate(item_pubDate(item))
|
|
|
|
xml.guid(@recent_items.url(item))
|
|
|
|
xml.link(@recent_items.url(item))
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-02-23 20:29:43 -05:00
|
|
|
|
|
|
|
{Learn more}[link:classes/ActionView/Base.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
* Filters for pre- and post-processing of the response
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
class WeblogController < ActionController::Base
|
2010-07-19 07:51:04 -04:00
|
|
|
# filters as methods
|
2004-11-23 20:04:44 -05:00
|
|
|
before_filter :authenticate, :cache, :audit
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
# filter as a proc
|
2008-01-03 16:05:12 -05:00
|
|
|
after_filter { |c| c.response.body = Gzip::compress(c.response.body) }
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
# class filter
|
2004-11-23 20:04:44 -05:00
|
|
|
after_filter LocalizeFilter
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2006-09-03 14:34:28 -04:00
|
|
|
def index
|
2004-11-23 20:04:44 -05:00
|
|
|
# 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
|
2005-11-07 04:51:47 -05:00
|
|
|
# exists, and the action will be logged for auditing.
|
2010-01-30 16:20:00 -05:00
|
|
|
|
|
|
|
# After this action has run, the output will first be localized then
|
2005-11-07 04:51:47 -05:00
|
|
|
# compressed to minimize bandwidth usage
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
|
|
|
def authenticate
|
2005-11-07 04:51:47 -05:00
|
|
|
# Implement the filter with full access to both request and response
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
{Learn more}[link:classes/ActionController/Filters/ClassMethods.html]
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
* Helpers for forms, dates, action links, and text
|
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
<%= text_field_tag "post", "title", "size" => 30 %>
|
2004-11-23 20:04:44 -05:00
|
|
|
<%= link_to "New post", :controller => "post", :action => "new" %>
|
2008-12-06 21:27:53 -05:00
|
|
|
<%= truncate(post.title, :length => 25) %>
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
{Learn more}[link:classes/ActionView/Helpers.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
* Layout sharing for template reuse
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
class WeblogController < ActionController::Base
|
|
|
|
layout "weblog_layout"
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def hello_world
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Layout file (called weblog_layout):
|
2006-04-29 12:34:23 -04:00
|
|
|
<html><body><%= yield %></body></html>
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
Template for hello_world action:
|
|
|
|
<h1>Hello world</h1>
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
Result of running hello_world action:
|
|
|
|
<html><body><h1>Hello world</h1></body></html>
|
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
{Learn more}[link:classes/ActionController/Layout/ClassMethods.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
* Routing makes pretty URLs incredibly easy
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
match 'clients/:client_name/:project_name/:controller/:action'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
Accessing "/clients/37signals/basecamp/project/index" calls ProjectController#index with
|
|
|
|
{ "client_name" => "37signals", "project_name" => "basecamp" } in `params`
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
From that action, you can write the redirect in a number of ways:
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
redirect_to(:action => "edit") =>
|
2010-07-19 07:51:04 -04:00
|
|
|
/clients/37signals/basecamp/project/edit
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
redirect_to(:client_name => "nextangle", :project_name => "rails") =>
|
2010-07-19 07:51:04 -04:00
|
|
|
/clients/nextangle/rails/project/index
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-19 07:51:04 -04:00
|
|
|
{Learn more}[link:classes/ActionDispatch/Routing.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2008-07-16 08:00:36 -04:00
|
|
|
* Easy testing of both controller and rendered template through ActionController::TestCase
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-07-16 08:00:36 -04:00
|
|
|
class LoginControllerTest < ActionController::TestCase
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_failing_authenticate
|
2005-10-16 11:42:03 -04:00
|
|
|
process :authenticate, :user_name => "nop", :password => ""
|
|
|
|
assert flash.has_key?(:alert)
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_redirected_to :action => "index"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-16 08:00:36 -04:00
|
|
|
{Learn more}[link:classes/ActionController/TestCase.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
* Automated benchmarking and integrated logging
|
|
|
|
|
2010-06-20 13:36:46 -04:00
|
|
|
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
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
If Active Record is used as the model, you'll have the database debugging
|
|
|
|
as well:
|
|
|
|
|
2010-06-20 13:36:46 -04:00
|
|
|
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')
|
2008-12-06 21:27:53 -05:00
|
|
|
Redirected to http://example.com/posts/5
|
2010-06-20 13:36:46 -04:00
|
|
|
Completed 302 Found in 221ms (Views: 215ms | ActiveRecord: 0.6ms)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
* Caching at three levels of granularity (page, action, fragment)
|
|
|
|
|
|
|
|
class WeblogController < ActionController::Base
|
|
|
|
caches_page :show
|
|
|
|
caches_action :account
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
def show
|
2010-01-30 16:20:00 -05:00
|
|
|
# the output of the method will be cached as
|
2005-02-23 20:29:43 -05:00
|
|
|
# ActionController::Base.page_cache_directory + "/weblog/show/n.html"
|
|
|
|
# and the web server will pick it up without even hitting Rails
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
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
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
def update
|
2005-10-16 11:42:03 -04:00
|
|
|
List.update(params[:list][:id], params[:list])
|
|
|
|
expire_page :action => "show", :id => params[:list][:id]
|
2005-02-23 20:29:43 -05:00
|
|
|
expire_action :action => "account"
|
2005-10-16 11:42:03 -04:00
|
|
|
redirect_to :action => "show", :id => params[:list][:id]
|
2005-02-23 20:29:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
{Learn more}[link:classes/ActionController/Caching.html]
|
|
|
|
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
* Powerful debugging mechanism for local requests
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
{Learn more}[link:classes/ActionController/Rescue.html]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2006-09-03 14:34:28 -04:00
|
|
|
== Simple example (from outside of Rails)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
This example will implement a simple weblog system using inline templates and
|
2005-02-23 20:29:43 -05:00
|
|
|
an Active Record model. So let's build that WeblogController with just a few
|
2004-11-23 20:04:44 -05:00
|
|
|
methods:
|
|
|
|
|
|
|
|
require 'action_controller'
|
|
|
|
require 'post'
|
2005-02-23 20:29:43 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
class WeblogController < ActionController::Base
|
|
|
|
layout "weblog/layout"
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def index
|
2010-12-19 14:37:33 -05:00
|
|
|
@posts = Post.all
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2008-12-06 21:27:53 -05:00
|
|
|
def show
|
2006-04-29 12:34:23 -04:00
|
|
|
@post = Post.find(params[:id])
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def new
|
|
|
|
@post = Post.new
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def create
|
2005-10-16 11:42:03 -04:00
|
|
|
@post = Post.create(params[:post])
|
2008-12-06 21:27:53 -05:00
|
|
|
redirect_to :action => "show", :id => @post.id
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
WeblogController::Base.view_paths = [ File.dirname(__FILE__) ]
|
2004-11-23 20:04:44 -05:00
|
|
|
WeblogController.process_cgi if $0 == __FILE__
|
|
|
|
|
|
|
|
The last two lines are responsible for telling ActionController where the
|
|
|
|
template files are located and actually running the controller on a new
|
2011-08-05 04:08:53 -04:00
|
|
|
request from the web-server (e.g., Apache).
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
And the templates look like this:
|
|
|
|
|
2008-12-06 21:27:53 -05:00
|
|
|
weblog/layout.html.erb:
|
2004-11-23 20:04:44 -05:00
|
|
|
<html><body>
|
2006-04-29 12:34:23 -04:00
|
|
|
<%= yield %>
|
2004-11-23 20:04:44 -05:00
|
|
|
</body></html>
|
|
|
|
|
2008-12-06 21:27:53 -05:00
|
|
|
weblog/index.html.erb:
|
2011-05-18 23:29:57 -04:00
|
|
|
<% @posts.each do |post| %>
|
2008-12-06 21:27:53 -05:00
|
|
|
<p><%= link_to(post.title, :action => "show", :id => post.id) %></p>
|
2004-11-23 20:04:44 -05:00
|
|
|
<% end %>
|
|
|
|
|
2008-12-06 21:27:53 -05:00
|
|
|
weblog/show.html.erb:
|
2004-11-23 20:04:44 -05:00
|
|
|
<p>
|
2008-12-06 21:27:53 -05:00
|
|
|
<b><%= @post.title %></b><br/>
|
|
|
|
<b><%= @post.content %></b>
|
2004-11-23 20:04:44 -05:00
|
|
|
</p>
|
|
|
|
|
2008-12-06 21:27:53 -05:00
|
|
|
weblog/new.html.erb:
|
2004-11-23 20:04:44 -05:00
|
|
|
<%= form "post" %>
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
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
|
2005-11-07 04:51:47 -05:00
|
|
|
Record model to make the new screen, which in turn hands everything over to
|
2004-11-23 20:04:44 -05:00
|
|
|
the create action (that's the default target for the form builder when given a
|
2008-12-06 21:27:53 -05:00
|
|
|
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).
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
2010-07-18 08:58:40 -04:00
|
|
|
== Download and installation
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2011-08-05 04:34:43 -04:00
|
|
|
The latest version of Action Pack can be installed with RubyGems:
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-18 08:58:40 -04:00
|
|
|
% [sudo] gem install actionpack
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-18 08:58:40 -04:00
|
|
|
Source code can be downloaded as part of the Rails project on GitHub
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2011-08-04 03:03:56 -04:00
|
|
|
* https://github.com/rails/rails/tree/master/actionpack
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
== License
|
|
|
|
|
2005-02-23 21:07:02 -05:00
|
|
|
Action Pack is released under the MIT license.
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
== Support
|
|
|
|
|
2010-07-18 08:58:40 -04:00
|
|
|
API documentation is at
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2011-05-24 11:42:01 -04:00
|
|
|
* http://api.rubyonrails.org
|
2010-07-18 08:58:40 -04:00
|
|
|
|
|
|
|
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
|
|
|
|
|
2011-05-10 12:30:06 -04:00
|
|
|
* https://github.com/rails/rails/issues
|