public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
1c16649b » dhh 2006-03-10 Added better support for us... 1 require 'action_controller/mime_type'
db045dbb » dhh 2004-11-23 Initial 2 require 'action_controller/request'
3 require 'action_controller/response'
b1999be5 » dhh 2005-02-14 A hopefully more successful... 4 require 'action_controller/routing'
865b1757 » dhh 2006-07-31 Added map.resources from th... 5 require 'action_controller/resources'
db045dbb » dhh 2004-11-23 Initial 6 require 'action_controller/url_rewriter'
df79e135 » dhh 2005-01-08 Added first stab at page an... 7 require 'drb'
22d9bad8 » jeremy 2005-10-20 Expose the session model ba... 8 require 'set'
db045dbb » dhh 2004-11-23 Initial 9
10 module ActionController #:nodoc:
11 class ActionControllerError < StandardError #:nodoc:
12 end
13 class SessionRestoreError < ActionControllerError #:nodoc:
14 end
15 class MissingTemplate < ActionControllerError #:nodoc:
16 end
8e56f5ea » dhh 2005-06-24 Improved performance of Rou... 17 class RoutingError < ActionControllerError #:nodoc:
b1999be5 » dhh 2005-02-14 A hopefully more successful... 18 attr_reader :failures
19 def initialize(message, failures=[])
20 super(message)
21 @failures = failures
22 end
23 end
24 class UnknownController < ActionControllerError #:nodoc:
25 end
db045dbb » dhh 2004-11-23 Initial 26 class UnknownAction < ActionControllerError #:nodoc:
27 end
250a570c » dhh 2005-01-01 Added class declaration for... 28 class MissingFile < ActionControllerError #:nodoc:
29 end
48fd667b » Marcel Molina 2005-10-15 Raise an exception if an at... 30 class SessionOverflowError < ActionControllerError #:nodoc:
31 DEFAULT_MESSAGE = 'Your session data is larger than the data column in which it is to be stored. You must increase the size of your data column if you intend to store large data.'
0abaf3a2 » jeremy 2005-11-08 CGI::Session::ActiveRecordS... 32
33 def initialize(message = nil)
34 super(message || DEFAULT_MESSAGE)
35 end
48fd667b » Marcel Molina 2005-10-15 Raise an exception if an at... 36 end
dab360e1 » dhh 2005-05-21 Added DoubleRenderError exc... 37 class DoubleRenderError < ActionControllerError #:nodoc:
82668678 » jamis 2005-07-09 Improved error message for ... 38 DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and only once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\". Finally, note that to cause a before filter to halt execution of the rest of the filter chain, the filter must return false, explicitly, so \"render(...) and return false\"."
39
0abaf3a2 » jeremy 2005-11-08 CGI::Session::ActiveRecordS... 40 def initialize(message = nil)
82668678 » jamis 2005-07-09 Improved error message for ... 41 super(message || DEFAULT_MESSAGE)
42 end
dab360e1 » dhh 2005-05-21 Added DoubleRenderError exc... 43 end
ea30f735 » dhh 2006-01-21 Raise a RedirectBackError i... 44 class RedirectBackError < ActionControllerError #:nodoc:
45 DEFAULT_MESSAGE = 'No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify @request.env["HTTP_REFERER"].'
46
47 def initialize(message = nil)
48 super(message || DEFAULT_MESSAGE)
49 end
50 end
db045dbb » dhh 2004-11-23 Initial 51
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 52 # Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed
53 # on request and then either render a template or redirect to another action. An action is defined as a public method
54 # on the controller, which will automatically be made accessible to the web-server through Rails Routes.
55 #
56 # A sample controller could look like this:
db045dbb » dhh 2004-11-23 Initial 57 #
58 # class GuestBookController < ActionController::Base
59 # def index
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 60 # @entries = Entry.find(:all)
db045dbb » dhh 2004-11-23 Initial 61 # end
62 #
63 # def sign
1c057b72 » jamis 2005-10-16 Update/clean up AP document... 64 # Entry.create(params[:entry])
db045dbb » dhh 2004-11-23 Initial 65 # redirect_to :action => "index"
66 # end
67 # end
68 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 69 # Actions, by default, render a template in the <tt>app/views</tt> directory corresponding to the name of the controller and action
70 # after executing code in the action. For example, the +index+ action of the +GuestBookController+ would render the
71 # template <tt>app/views/guestbook/index.rhtml</tt> by default after populating the <tt>@entries</tt> instance variable.
db045dbb » dhh 2004-11-23 Initial 72 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 73 # Unlike index, the sign action will not render a template. After performing its main purpose (creating a
74 # new entry in the guest book), it initiates a redirect instead. This redirect works by returning an external
75 # "302 Moved" HTTP response that takes the user to the index action.
db045dbb » dhh 2004-11-23 Initial 76 #
77 # The index and sign represent the two basic action archetypes used in Action Controllers. Get-and-show and do-and-redirect.
78 # Most actions are variations of these themes.
79 #
80 # == Requests
81 #
82 # Requests are processed by the Action Controller framework by extracting the value of the "action" key in the request parameters.
83 # This value should hold the name of the action to be performed. Once the action has been identified, the remaining
84 # request parameters, the session (if one is available), and the full request with all the http headers are made available to
85 # the action through instance variables. Then the action is performed.
86 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 87 # The full request object is available with the request accessor and is primarily used to query for http headers. These queries
88 # are made by accessing the environment hash, like this:
db045dbb » dhh 2004-11-23 Initial 89 #
0cd883e1 » jeremy 2006-05-17 Change the request.env exam... 90 # def server_ip
91 # location = request.env["SERVER_ADDR"]
92 # render :text => "This server hosted at #{location}"
db045dbb » dhh 2004-11-23 Initial 93 # end
94 #
95 # == Parameters
96 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 97 # All request parameters, whether they come from a GET or POST request, or from the URL, are available through the params method
98 # which returns a hash. For example, an action that was performed through <tt>/weblog/list?category=All&limit=5</tt> will include
99 # <tt>{ "category" => "All", "limit" => 5 }</tt> in params.
db045dbb » dhh 2004-11-23 Initial 100 #
101 # It's also possible to construct multi-dimensional parameter hashes by specifying keys using brackets, such as:
102 #
103 # <input type="text" name="post[name]" value="david">
104 # <input type="text" name="post[address]" value="hyacintvej">
105 #
e4efcfd4 » dhh 2005-02-23 Updated documentation 106 # A request stemming from a form holding these inputs will include <tt>{ "post" => { "name" => "david", "address" => "hyacintvej" } }</tt>.
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 107 # If the address input had been named "post[address][street]", the params would have included
e4efcfd4 » dhh 2005-02-23 Updated documentation 108 # <tt>{ "post" => { "address" => { "street" => "hyacintvej" } } }</tt>. There's no limit to the depth of the nesting.
db045dbb » dhh 2004-11-23 Initial 109 #
110 # == Sessions
111 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 112 # Sessions allows you to store objects in between requests. This is useful for objects that are not yet ready to be persisted,
db045dbb » dhh 2004-11-23 Initial 113 # such as a Signup object constructed in a multi-paged process, or objects that don't change much and are needed all the time, such
114 # as a User object for a system that requires login. The session should not be used, however, as a cache for objects where it's likely
115 # they could be changed unknowingly. It's usually too much work to keep it all synchronized -- something databases already excel at.
116 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 117 # You can place objects in the session by using the <tt>session</tt> method, which accesses a hash:
db045dbb » dhh 2004-11-23 Initial 118 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 119 # session[:person] = Person.authenticate(user_name, password)
db045dbb » dhh 2004-11-23 Initial 120 #
121 # And retrieved again through the same hash:
122 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 123 # Hello #{session[:person]}
db045dbb » dhh 2004-11-23 Initial 124 #
46c4fd41 » jamis 2005-09-01 ActionController documentat... 125 # For removing objects from the session, you can either assign a single key to nil, like <tt>session[:person] = nil</tt>, or you can
9c605227 » dhh 2005-03-20 Added a bit more to the ses... 126 # remove the entire session with reset_session.
127 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 128 # By default, sessions are stored on the file system in <tt>RAILS_ROOT/tmp/sessions</tt>. Any object can be placed in the session
129 # (as long as it can be Marshalled). But remember that 1000 active sessions each storing a 50kb object could lead to a 50MB store on the filesystem.
130 # In other words, think carefully about size and caching before resorting to the use of the session on the filesystem.
131 #
132 # An alternative to storing sessions on disk is to use ActiveRecordStore to store sessions in your database, which can solve problems
133 # caused by storing sessions in the file system and may speed up your application. To use ActiveRecordStore, uncomment the line:
134 #
135 # config.action_controller.session_store = :active_record_store
136 #
137 # in your <tt>environment.rb</tt> and run <tt>rake db:sessions:create</tt>.
138 #
db045dbb » dhh 2004-11-23 Initial 139 # == Responses
140 #
141 # Each action results in a response, which holds the headers and document to be sent to the user's browser. The actual response
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 142 # object is generated automatically through the use of renders and redirects and requires no user intervention.
db045dbb » dhh 2004-11-23 Initial 143 #
144 # == Renders
145 #
146 # Action Controller sends content to the user by using one of five rendering methods. The most versatile and common is the rendering
147 # of a template. Included in the Action Pack is the Action View, which enables rendering of ERb templates. It's automatically configured.
148 # The controller passes objects to the view by assigning instance variables:
149 #
150 # def show
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 151 # @post = Post.find(params[:id])
db045dbb » dhh 2004-11-23 Initial 152 # end
153 #
154 # Which are then automatically available to the view:
155 #
156 # Title: <%= @post.title %>
157 #
158 # You don't have to rely on the automated rendering. Especially actions that could result in the rendering of different templates will use
159 # the manual rendering methods:
160 #
161 # def search
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 162 # @results = Search.find(params[:query])
db045dbb » dhh 2004-11-23 Initial 163 # case @results
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 164 # when 0 then render :action => "no_results"
165 # when 1 then render :action => "show"
166 # when 2..10 then render :action => "show_many"
db045dbb » dhh 2004-11-23 Initial 167 # end
168 # end
169 #
170 # Read more about writing ERb and Builder templates in link:classes/ActionView/Base.html.
171 #
172 # == Redirects
173 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 174 # Redirects are used to move from one action to another. For example, after a <tt>create</tt> action, which stores a blog entry to a database,
175 # we might like to show the user the new entry. Because we're following good DRY principles (Don't Repeat Yourself), we're going to reuse (and redirect to)
176 # a <tt>show</tt> action that we'll assume has already been created. The code might look like this:
177 #
178 # def create
179 # @entry = Entry.new(params[:entry])
180 # if @entry.save
181 # # The entry was saved correctly, redirect to show
182 # redirect_to :action => 'show', :id => @entry.id
183 # else
184 # # things didn't go so well, do something else
185 # end
186 # end
db045dbb » dhh 2004-11-23 Initial 187 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 188 # In this case, after saving our new entry to the database, the user is redirected to the <tt>show</tt> method which is then executed.
db045dbb » dhh 2004-11-23 Initial 189 #
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 190 # == Calling multiple redirects or renders
191 #
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 192 # An action should conclude with a single render or redirect. Attempting to try to do either again will result in a DoubleRenderError:
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 193 #
194 # def do_something
195 # redirect_to :action => "elsewhere"
14e7c7c2 » dhh 2005-07-09 Better documentation for Ca... 196 # render :action => "overthere" # raises DoubleRenderError
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 197 # end
198 #
14e7c7c2 » dhh 2005-07-09 Better documentation for Ca... 199 # If you need to redirect on the condition of something, then be sure to add "and return" to halt execution.
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 200 #
14e7c7c2 » dhh 2005-07-09 Better documentation for Ca... 201 # def do_something
202 # redirect_to(:action => "elsewhere") and return if monkeys.nil?
203 # render :action => "overthere" # won't be called unless monkeys is nil
46c4fd41 » jamis 2005-09-01 ActionController documentat... 204 # end
205 #
db045dbb » dhh 2004-11-23 Initial 206 class Base
207 DEFAULT_RENDER_STATUS_CODE = "200 OK"
1bce58b3 » seckar 2006-02-01 Add Reloadable::OnlySubclas... 208
5cbc062c » dhh 2006-02-04 Just go with Subclasses ins... 209 include Reloadable::Subclasses
1bce58b3 » seckar 2006-02-01 Add Reloadable::OnlySubclas... 210
db045dbb » dhh 2004-11-23 Initial 211 # Determines whether the view has access to controller internals @request, @response, @session, and @template.
212 # By default, it does.
213 @@view_controller_internals = true
214 cattr_accessor :view_controller_internals
215
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 216 # Protected instance variable cache
217 @@protected_variables_cache = nil
218 cattr_accessor :protected_variables_cache
219
c971c248 » dhh 2005-05-05 Changed RAILS_ASSET_HOST to... 220 # Prepends all the URL-generating helpers from AssetHelper. This makes it possible to easily move javascripts, stylesheets,
221 # and images to a dedicated asset server away from the main web server. Example:
222 # ActionController::Base.asset_host = "http://assets.example.com"
223 @@asset_host = ""
224 cattr_accessor :asset_host
225
db045dbb » dhh 2004-11-23 Initial 226 # All requests are considered local by default, so everyone will be exposed to detailed debugging screens on errors.
227 # When the application is ready to go public, this should be set to false, and the protected method <tt>local_request?</tt>
228 # should instead be implemented in the controller to determine when debugging screens should be shown.
229 @@consider_all_requests_local = true
230 cattr_accessor :consider_all_requests_local
b1999be5 » dhh 2005-02-14 A hopefully more successful... 231
232 # Enable or disable the collection of failure information for RoutingErrors.
233 # This information can be extremely useful when tweaking custom routes, but is
234 # pointless once routes have been tested and verified.
235 @@debug_routes = true
236 cattr_accessor :debug_routes
db045dbb » dhh 2004-11-23 Initial 237
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 238 # Controls whether the application is thread-safe, so multi-threaded servers like WEBrick know whether to apply a mutex
0c3298f2 » dhh 2005-06-22 Added ActionController::Bas... 239 # around the performance of each action. Action Pack and Active Record are by default thread-safe, but many applications
240 # may not be. Turned off by default.
241 @@allow_concurrency = false
242 cattr_accessor :allow_concurrency
243
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 244 # Modern REST web services often need to submit complex data to the web application.
c450a36f » dhh 2006-03-05 Doc fixes 245 # The param_parsers hash lets you register handlers wich will process the http body and add parameters to the
09095c72 » Marcel Molina 2006-04-25 Remove all remaining refere... 246 # <tt>params</tt> hash. These handlers are invoked for post and put requests.
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 247 #
62dc792a » dhh 2006-04-04 CHANGED DEFAULT: Don't pars... 248 # By default application/xml is enabled. A XmlSimple class with the same param name as the root will be instanciated
09095c72 » Marcel Molina 2006-04-25 Remove all remaining refere... 249 # in the <tt>params</tt>. This allows XML requests to mask themselves as regular form submissions, so you can have one
62dc792a » dhh 2006-04-04 CHANGED DEFAULT: Don't pars... 250 # action serve both regular forms and web service requests.
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 251 #
dc4b5cff » dhh 2006-03-06 XmlSimple _should_ be the d... 252 # Example of doing your own parser for a custom content type:
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 253 #
de37f7df » dhh 2006-03-12 Mime types are separated by... 254 # ActionController::Base.param_parsers[Mime::Type.lookup('application/atom+xml')] = Proc.new do |data|
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 255 # node = REXML::Document.new(post)
256 # { node.root.name => node.root }
257 # end
258 #
dc4b5cff » dhh 2006-03-06 XmlSimple _should_ be the d... 259 # Note: Up until release 1.1 of Rails, Action Controller would default to using XmlSimple configured to discard the
260 # root node for such requests. The new default is to keep the root, such that "<r><name>David</name></r>" results
261 # in params[:r][:name] for "David" instead of params[:name]. To get the old behavior, you can
7bcf4c66 » jamis 2006-03-25 Enable application/x-yaml p... 262 # re-register XmlSimple as application/xml handler ike this:
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 263 #
62dc792a » dhh 2006-04-04 CHANGED DEFAULT: Don't pars... 264 # ActionController::Base.param_parsers[Mime::XML] =
dc4b5cff » dhh 2006-03-06 XmlSimple _should_ be the d... 265 # Proc.new { |data| XmlSimple.xml_in(data, 'ForceArray' => false) }
62dc792a » dhh 2006-04-04 CHANGED DEFAULT: Don't pars... 266 #
267 # A YAML parser is also available and can be turned on with:
268 #
269 # ActionController::Base.param_parsers[Mime::YAML] = :yaml
270 @@param_parsers = { Mime::XML => :xml_simple }
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 271 cattr_accessor :param_parsers
272
db045dbb » dhh 2004-11-23 Initial 273 # Template root determines the base from which template references will be made. So a call to render("test/template")
274 # will be converted to "#{template_root}/test/template.rhtml".
12a75736 » dhh 2005-02-20 Added new keyword to specif... 275 class_inheritable_accessor :template_root
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 276
db045dbb » dhh 2004-11-23 Initial 277 # The logger is used for generating information on the action run-time (including benchmarking) if available.
278 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
279 cattr_accessor :logger
280
281 # Determines which template class should be used by ActionController.
282 cattr_accessor :template_class
283
284 # Turn on +ignore_missing_templates+ if you want to unit test actions without making the associated templates.
285 cattr_accessor :ignore_missing_templates
286
287 # Holds the request object that's primarily used to get environment variables through access like
6ee06ebe » dhh 2005-04-16 Changed render_partial to t... 288 # <tt>request.env["REQUEST_URI"]</tt>.
db045dbb » dhh 2004-11-23 Initial 289 attr_accessor :request
290
46c4fd41 » jamis 2005-09-01 ActionController documentat... 291 # Holds a hash of all the GET, POST, and Url parameters passed to the action. Accessed like <tt>params["post_id"]</tt>
db045dbb » dhh 2004-11-23 Initial 292 # to get the post_id. No type casts are made, so all values are returned as strings.
293 attr_accessor :params
294
295 # Holds the response object that's primarily used to set additional HTTP headers through access like
6ee06ebe » dhh 2005-04-16 Changed render_partial to t... 296 # <tt>response.headers["Cache-Control"] = "no-cache"</tt>. Can also be used to access the final body HTML after a template
297 # has been rendered through response.body -- useful for <tt>after_filter</tt>s that wants to manipulate the output,
db045dbb » dhh 2004-11-23 Initial 298 # such as a OutputCompressionFilter.
299 attr_accessor :response
300
6ee06ebe » dhh 2005-04-16 Changed render_partial to t... 301 # Holds a hash of objects in the session. Accessed like <tt>session[:person]</tt> to get the object tied to the "person"
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 302 # key. The session will hold any type of object as values, but the key should be a string or symbol.
db045dbb » dhh 2004-11-23 Initial 303 attr_accessor :session
304
6ee06ebe » dhh 2005-04-16 Changed render_partial to t... 305 # Holds a hash of header names and values. Accessed like <tt>headers["Cache-Control"]</tt> to get the value of the Cache-Control
db045dbb » dhh 2004-11-23 Initial 306 # directive. Values should always be specified as strings.
307 attr_accessor :headers
308
309 # Holds the hash of variables that are passed on to the template class to be made available to the view. This hash
310 # is generated by taking a snapshot of all the instance variables in the current scope just before a template is rendered.
311 attr_accessor :assigns
312
c6f819ed » jeremy 2005-07-04 r2827@asus: jeremy | 2005... 313 # Returns the name of the action this controller is processing.
314 attr_accessor :action_name
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 315
db045dbb » dhh 2004-11-23 Initial 316 class << self
317 # Factory for the standard create, process loop where the controller is discarded after processing.
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 318 def process(request, response) #:nodoc:
319 new.process(request, response)
db045dbb » dhh 2004-11-23 Initial 320 end
0ffb6c16 » dhh 2004-12-06 Syntax errors and other exc... 321
db045dbb » dhh 2004-11-23 Initial 322 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
323 def controller_class_name
d2b9e39c » jeremy 2005-07-04 r2828@asus: jeremy | 2005... 324 @controller_class_name ||= name.demodulize
db045dbb » dhh 2004-11-23 Initial 325 end
326
327 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
328 def controller_name
d2b9e39c » jeremy 2005-07-04 r2828@asus: jeremy | 2005... 329 @controller_name ||= controller_class_name.sub(/Controller$/, '').underscore
db045dbb » dhh 2004-11-23 Initial 330 end
b1999be5 » dhh 2005-02-14 A hopefully more successful... 331
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 332 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat".
b1999be5 » dhh 2005-02-14 A hopefully more successful... 333 def controller_path
8ff44631 » seckar 2006-03-13 Simplify controller_path 334 @controller_path ||= name.gsub(/Controller$/, '').underscore
b1999be5 » dhh 2005-02-14 A hopefully more successful... 335 end
04b8bc1b » dhh 2005-02-17 Fixed that a bunch of metho... 336
337 # Return an array containing the names of public methods that have been marked hidden from the action processor.
338 # By default, all methods defined in ActionController::Base and included modules are hidden.
13779431 » dhh 2006-02-11 The components module shoul... 339 # More methods can be hidden using <tt>hide_actions</tt>.
04b8bc1b » dhh 2005-02-17 Fixed that a bunch of metho... 340 def hidden_actions
341 write_inheritable_attribute(:hidden_actions, ActionController::Base.public_instance_methods) unless read_inheritable_attribute(:hidden_actions)
342 read_inheritable_attribute(:hidden_actions)
343 end
344
345 # Hide each of the given methods from being callable as actions.
69f18a7a » dhh 2005-02-23 Keep the singular style, li... 346 def hide_action(*names)
13779431 » dhh 2006-02-11 The components module shoul... 347 write_inheritable_attribute(:hidden_actions, hidden_actions | names.collect { |n| n.to_s })
12a75736 » dhh 2005-02-20 Added new keyword to specif... 348 end
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 349
350 # Replace sensitive paramater data from the request log.
351 # Filters paramaters that have any of the arguments as a substring.
352 # Looks in all subhashes of the param hash for keys to filter.
353 # If a block is given, each key and value of the paramater hash and all
354 # subhashes is passed to it, the value or key
355 # can be replaced using String#replace or similar method.
356 #
357 # Examples:
358 # filter_parameter_logging
359 # => Does nothing, just slows the logging process down
360 #
361 # filter_parameter_logging :password
362 # => replaces the value to all keys matching /password/i with "[FILTERED]"
363 #
364 # filter_parameter_logging :foo, "bar"
365 # => replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
366 #
367 # filter_parameter_logging { |k,v| v.reverse! if k =~ /secret/i }
368 # => reverses the value to all keys matching /secret/i
369 #
370 # filter_parameter_logging(:foo, "bar") { |k,v| v.reverse! if k =~ /secret/i }
371 # => reverses the value to all keys matching /secret/i, and
372 # replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
373 def filter_parameter_logging(*filter_words, &block)
374 parameter_filter = Regexp.new(filter_words.collect{ |s| s.to_s }.join('|'), true) if filter_words.length > 0
375
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 376 define_method(:filter_parameters) do |unfiltered_parameters|
377 filtered_parameters = {}
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 378
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 379 unfiltered_parameters.each do |key, value|
380 if key =~ parameter_filter
381 filtered_parameters[key] = '[FILTERED]'
382 elsif value.is_a?(Hash)
383 filtered_parameters[key] = filter_parameters(value)
384 elsif block_given?
385 key, value = key.dup, value.dup
386 yield key, value
387 filtered_parameters[key] = value
388 else
389 filtered_parameters[key] = value
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 390 end
0049bd72 » Marcel Molina 2006-04-29 Update README 391 end
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 392
393 filtered_parameters
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 394 end
395 end
db045dbb » dhh 2004-11-23 Initial 396 end
397
76540822 » jeremy 2006-02-09 Major components cleanup an... 398 public
db045dbb » dhh 2004-11-23 Initial 399 # Extracts the action_name from the request parameters and performs that action.
0ffb6c16 » dhh 2004-12-06 Syntax errors and other exc... 400 def process(request, response, method = :perform_action, *arguments) #:nodoc:
db045dbb » dhh 2004-11-23 Initial 401 initialize_template_class(response)
402 assign_shortcuts(request, response)
403 initialize_current_url
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 404 assign_names
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 405 forget_variables_added_to_assigns
406
407 log_processing
0ffb6c16 » dhh 2004-12-06 Syntax errors and other exc... 408 send(method, *arguments)
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 409
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 410 response
ddf69109 » jeremy 2005-11-01 ensure close_session in AC:... 411 ensure
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 412 process_cleanup
db045dbb » dhh 2004-11-23 Initial 413 end
414
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 415 # Returns a URL that has been rewritten according to the options hash and the defined Routes.
416 # (For doing a complete redirect, use redirect_to).
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 417 #  
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 418 # <tt>url_for</tt> is used to:
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 419 #  
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 420 # All keys given to url_for are forwarded to the Route module, save for the following:
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 421 # * <tt>:anchor</tt> -- specifies the anchor name to be appended to the path. For example,
422 # <tt>url_for :controller => 'posts', :action => 'show', :id => 10, :anchor => 'comments'</tt>
423 # will produce "/posts/show/10#comments".
6ef5b747 » Marcel Molina 2006-05-14 Fix flip flopped logic in d... 424 # * <tt>:only_path</tt> -- if true, returns the relative URL (omitting the protocol, host name, and port) (<tt>false</tt> by default)
7a6a923f » dhh 2005-03-26 Added trailing_slash option... 425 # * <tt>:trailing_slash</tt> -- if true, adds a trailing slash, as in "/archive/2005/". Note that this
426 # is currently not recommended since it breaks caching.
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 427 # * <tt>:host</tt> -- overrides the default (current) host if provided
428 # * <tt>:protocol</tt> -- overrides the default (current) protocol if provided
e4efcfd4 » dhh 2005-02-23 Updated documentation 429 #
41ea6963 » dhh 2005-02-23 Added Base#render_to_string... 430 # The URL is generated from the remaining keys in the hash. A URL contains two key parts: the <base> and a query string.
431 # Routes composes a query string as the key/value pairs not included in the <base>.
e4efcfd4 » dhh 2005-02-23 Updated documentation 432 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 433 # The default Routes setup supports a typical Rails path of "controller/action/id" where action and id are optional, with
434 # action defaulting to 'index' when not given. Here are some typical url_for statements and their corresponding URLs:
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 435 #  
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 436 # url_for :controller => 'posts', :action => 'recent' # => 'proto://host.com/posts/recent'
437 # url_for :controller => 'posts', :action => 'index' # => 'proto://host.com/posts'
438 # url_for :controller => 'posts', :action => 'show', :id => 10 # => 'proto://host.com/posts/show/10'
e4efcfd4 » dhh 2005-02-23 Updated documentation 439 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 440 # When generating a new URL, missing values may be filled in from the current request's parameters. For example,
441 # <tt>url_for :action => 'some_action'</tt> will retain the current controller, as expected. This behavior extends to
442 # other parameters, including <tt>:controller</tt>, <tt>:id</tt>, and any other parameters that are placed into a Route's
443 # path.
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 444 #  
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 445 # The URL helpers such as <tt>url_for</tt> have a limited form of memory: when generating a new URL, they can look for
446 # missing values in the current request's parameters. Routes attempts to guess when a value should and should not be
447 # taken from the defaults. There are a few simple rules on how this is performed:
e4efcfd4 » dhh 2005-02-23 Updated documentation 448 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 449 # * If the controller name begins with a slash, no defaults are used: <tt>url_for :controller => '/home'</tt>
450 # * If the controller changes, the action will default to index unless provided
e4efcfd4 » dhh 2005-02-23 Updated documentation 451 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 452 # The final rule is applied while the URL is being generated and is best illustrated by an example. Let us consider the
453 # route given by <tt>map.connect 'people/:last/:first/:action', :action => 'bio', :controller => 'people'</tt>.
e4efcfd4 » dhh 2005-02-23 Updated documentation 454 #
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 455 # Suppose that the current URL is "people/hh/david/contacts". Let's consider a few different cases of URLs which are generated
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 456 # from this page.
e4efcfd4 » dhh 2005-02-23 Updated documentation 457 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 458 # * <tt>url_for :action => 'bio'</tt> -- During the generation of this URL, default values will be used for the first and
537efa36 » dhh 2005-04-30 Doc fix #1200 459 # last components, and the action shall change. The generated URL will be, "people/hh/david/bio".
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 460 # * <tt>url_for :first => 'davids-little-brother'</tt> This generates the URL 'people/hh/davids-little-brother' -- note
461 # that this URL leaves out the assumed action of 'bio'.
e4efcfd4 » dhh 2005-02-23 Updated documentation 462 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 463 # However, you might ask why the action from the current request, 'contacts', isn't carried over into the new URL. The
464 # answer has to do with the order in which the parameters appear in the generated path. In a nutshell, since the
465 # value that appears in the slot for <tt>:first</tt> is not equal to default value for <tt>:first</tt> we stop using
466 # defaults. On it's own, this rule can account for much of the typical Rails URL behavior.
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 467 #  
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 468 # Although a convienence, defaults can occasionaly get in your way. In some cases a default persists longer than desired.
469 # The default may be cleared by adding <tt>:name => nil</tt> to <tt>url_for</tt>'s options.
470 # This is often required when writing form helpers, since the defaults in play may vary greatly depending upon where the
471 # helper is used from. The following line will redirect to PostController's default action, regardless of the page it is
472 # displayed on:
db045dbb » dhh 2004-11-23 Initial 473 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 474 # url_for :controller => 'posts', :action => nil
475 #
c8e0e10e » dhh 2005-09-09 Added documentation for ove... 476 # If you explicitly want to create a URL that's almost the same as the current URL, you can do so using the
477 # :overwrite_params options. Say for your posts you have different views for showing and printing them.
478 # Then, in the show view, you get the URL for the print view like this
479 #
480 # url_for :overwrite_params => { :action => 'print' }
481 #
482 # This takes the current URL as is and only exchanges the action. In contrast, <tt>url_for :action => 'print'</tt>
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 483 # would have slashed-off the path components after the changed action.
db045dbb » dhh 2004-11-23 Initial 484 def url_for(options = {}, *parameters_for_method_reference) #:doc:
485 case options
486 when String then options
487 when Symbol then send(options, *parameters_for_method_reference)
488 when Hash then @url.rewrite(rewrite_options(options))
489 end
490 end
491
492 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
493 def controller_class_name
494 self.class.controller_class_name
495 end
496
497 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
498 def controller_name
499 self.class.controller_name
500 end
501
bea737eb » sstephenson 2005-11-22 Make ActionController's ren... 502 def session_enabled?
503 request.session_options[:disabled] != false
504 end
505
db045dbb » dhh 2004-11-23 Initial 506 protected
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 507 # Renders the content that will be returned to the browser as the response body.
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 508 #
509 # === Rendering an action
510 #
511 # Action rendering is the most common form and the type used automatically by Action Controller when nothing else is
512 # specified. By default, actions are rendered within the current layout (if one exists).
513 #
514 # # Renders the template for the action "goal" within the current controller
515 # render :action => "goal"
516 #
517 # # Renders the template for the action "short_goal" within the current controller,
518 # # but without the current active layout
519 # render :action => "short_goal", :layout => false
520 #
521 # # Renders the template for the action "long_goal" within the current controller,
522 # # but with a custom layout
1aab0e2c » dhh 2005-07-21 Doc fixes #1775, #1776 [jon... 523 # render :action => "long_goal", :layout => "spectacular"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 524 #
525 # _Deprecation_ _notice_: This used to have the signatures <tt>render_action("action", status = 200)</tt>,
526 # <tt>render_without_layout("controller/action", status = 200)</tt>, and
527 # <tt>render_with_layout("controller/action", status = 200, layout)</tt>.
528 #
529 # === Rendering partials
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 530 #
531 # Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 532 # without reloading. Rendering of partials from the controller makes it possible to use the same partial template in
533 # both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the
534 # controller action responding to Ajax calls). By default, the current layout is not used.
535 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 536 # # Renders the same partial with a local variable.
537 # render :partial => "person", :locals => { :name => "david" }
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 538 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 539 # # Renders a collection of the same partial by making each element
540 # # of @winners available through the local variable "person" as it
541 # # builds the complete response.
542 # render :partial => "person", :collection => @winners
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 543 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 544 # # Renders the same collection of partials, but also renders the
545 # # person_divider partial between each person partial.
546 # render :partial => "person", :collection => @winners, :spacer_template => "person_divider"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 547 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 548 # # Renders a collection of partials located in a view subfolder
549 # # outside of our current controller. In this example we will be
550 # # rendering app/views/shared/_note.r(html|xml) Inside the partial
551 # # each element of @new_notes is available as the local var "note".
552 # render :partial => "shared/note", :collection => @new_notes
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 553 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 554 # # Renders the partial with a status code of 500 (internal error).
555 # render :partial => "broken", :status => 500
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 556 #
cf965cda » jeremy 2006-07-07 Clarify partial filename co... 557 # Note that the partial filename must also be a valid Ruby variable name,
558 # so e.g. 2005 and register-user are invalid.
559 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 560 # _Deprecation_ _notice_: This used to have the signatures
561 # <tt>render_partial(partial_path = default_template_name, object = nil, local_assigns = {})</tt> and
562 # <tt>render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = {})</tt>.
563 #
f510b09c » dhh 2005-10-03 Made the documentation abou... 564 # === Rendering a template
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 565 #
f510b09c » dhh 2005-10-03 Made the documentation abou... 566 # Template rendering works just like action rendering except that it takes a path relative to the template root.
567 # The current layout is automatically applied.
fede0f57 » dhh 2005-07-11 Fixed documentation for :ac... 568 #
569 # # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.rhtml)
f510b09c » dhh 2005-10-03 Made the documentation abou... 570 # render :template => "weblog/show"
571 #
572 # === Rendering a file
573 #
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 574 # File rendering works just like action rendering except that it takes a filesystem path. By default, the path
575 # is assumed to be absolute, and the current layout is not applied.
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 576 #
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 577 # # Renders the template located at the absolute filesystem path
578 # render :file => "/path/to/some/template.rhtml"
579 # render :file => "c:/path/to/some/template.rhtml"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 580 #
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 581 # # Renders a template within the current layout, and with a 404 status code
582 # render :file => "/path/to/some/template.rhtml", :layout => true, :status => 404
583 # render :file => "c:/path/to/some/template.rhtml", :layout => true, :status => 404
584 #
585 # # Renders a template relative to the template root and chooses the proper file extension
586 # render :file => "some/template", :use_full_path => true
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 587 #
588 # _Deprecation_ _notice_: This used to have the signature <tt>render_file(path, status = 200)</tt>
589 #
590 # === Rendering text
591 #
592 # Rendering of text is usually used for tests or for rendering prepared content, such as a cache. By default, text
593 # rendering is not done within the active layout.
594 #
595 # # Renders the clear text "hello world" with status code 200
596 # render :text => "hello world!"
597 #
598 # # Renders the clear text "Explosion!" with status code 500
599 # render :text => "Explosion!", :status => 500
600 #
601 # # Renders the clear text "Hi there!" within the current active layout (if one exists)
602 # render :text => "Explosion!", :layout => true
603 #
01cfd2b0 » dhh 2005-11-07 Fix docs (closes #2725) 604 # # Renders the clear text "Hi there!" within the layout
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 605 # # placed in "app/views/layouts/special.r(html|xml)"
606 # render :text => "Explosion!", :layout => "special"
607 #
40f50fd7 » dhh 2006-07-04 Doc fix (closes #5429) 608 # The :text option can also accept a Proc object, which can be used to manually control the page generation. This should
609 # generally be avoided, as it violates the separation between code and content, and because almost everything that can be
610 # done with this method can also be done more cleanly using one of the other rendering methods, most notably templates.
611 #
612 # # Renders "Hello from code!"
613 # render :text => proc { |response, output| output.write("Hello from code!") }
614 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 615 # _Deprecation_ _notice_: This used to have the signature <tt>render_text("text", status = 200)</tt>
616 #
617 # === Rendering an inline template
618 #
619 # Rendering of an inline template works as a cross between text and action rendering where the source for the template
620 # is supplied inline, like text, but its interpreted with ERb or Builder, like action. By default, ERb is used for rendering
621 # and the current layout is not used.
622 #
623 # # Renders "hello, hello, hello, again"
624 # render :inline => "<%= 'hello, ' * 3 + 'again' %>"
625 #
626 # # Renders "<p>Good seeing you!</p>" using Builder
627 # render :inline => "xml.p { 'Good seeing you!' }", :type => :rxml
628 #
629 # # Renders "hello david"
630 # render :inline => "<%= 'hello ' + name %>", :locals => { :name => "david" }
631 #
632 # _Deprecation_ _notice_: This used to have the signature <tt>render_template(template, status = 200, type = :rhtml)</tt>
633 #
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 634 # === Rendering inline JavaScriptGenerator page updates
635 #
636 # In addition to rendering JavaScriptGenerator page updates with Ajax in RJS templates (see ActionView::Base for details),
637 # you can also pass the <tt>:update</tt> parameter to +render+, along with a block, to render page updates inline.
638 #
639 # render :update do |page|
640 # page.replace_html 'user_list', :partial => 'user', :collection => @users
641 # page.visual_effect :highlight, 'user_list'
642 # end
643 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 644 # === Rendering nothing
645 #
646 # Rendering nothing is often convenient in combination with Ajax calls that perform their effect client-side or
cb0f8fda » dhh 2005-07-12 Worked around a Safari bug ... 647 # when you just want to communicate a status code. Due to a bug in Safari, nothing actually means a single space.
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 648 #
649 # # Renders an empty response with status code 200
650 # render :nothing => true
651 #
652 # # Renders an empty response with status code 401 (access denied)
653 # render :nothing => true, :status => 401
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 654 def render(options = nil, deprecated_status = nil, &block) #:doc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 655 raise DoubleRenderError, "Can only render or redirect once per action" if performed?
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 656
657 # Backwards compatibility
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 658 unless options.is_a?(Hash)
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 659 if options == :update
660 options = {:update => true}
661 else
662 return render_file(options || default_template_name, deprecated_status, true)
663 end
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 664 end
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 665
06c2b43f » dhh 2006-03-12 Rendering xml shouldnt happ... 666 if content_type = options[:content_type]
5c303523 » dhh 2006-06-01 Added support for Mime obje... 667 headers["Content-Type"] = content_type.to_s
06c2b43f » dhh 2006-03-12 Rendering xml shouldnt happ... 668 end
669
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 670 if text = options[:text]
671 render_text(text, options[:status])
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 672
673 else
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 674 if file = options[:file]
bea737eb » sstephenson 2005-11-22 Make ActionController's ren... 675 render_file(file, options[:status], options[:use_full_path], options[:locals] || {})
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 676
677 elsif template = options[:template]
678 render_file(template, options[:status], true)
679
680 elsif inline = options[:inline]
43b6a74f » dhh 2005-10-13 Added :locals support for r... 681 render_template(inline, options[:status], options[:type], options[:locals] || {})
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 682
683 elsif action_name = options[:action]
684 render_action(action_name, options[:status], options[:layout])
1c16649b » dhh 2006-03-10 Added better support for us... 685
686 elsif xml = options[:xml]
687 render_xml(xml, options[:status])
688
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 689 elsif partial = options[:partial]
690 partial = default_template_name if partial == true
691 if collection = options[:collection]
692 render_partial_collection(partial, collection, options[:spacer_template], options[:locals], options[:status])
693 else
e3c02d8c » dhh 2005-09-09 Fixed that render :partial ... 694 render_partial(partial, ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals], options[:status])
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 695 end
696
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 697 elsif options[:update]
d2adec43 » seckar 2006-01-29 Ensure assigns are copied t... 698 add_variables_to_assigns
699 @template.send :evaluate_assigns
700
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 701 generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block)
702 render_javascript(generator.to_s)
703
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 704 elsif options[:nothing]
705 # Safari doesn't pass the headers of the return if the response is zero length
706 render_text(" ", options[:status])
707
708 else
709 render_file(default_template_name, options[:status], true)
710
711 end
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 712 end
713 end
db045dbb » dhh 2004-11-23 Initial 714
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 715 # Renders according to the same rules as <tt>render</tt>, but returns the result in a string instead
716 # of sending it as the response body to the browser.
f9937dd0 » sstephenson 2006-01-20 Pass along blocks from rend... 717 def render_to_string(options = nil, &block) #:doc:
718 result = render(options, &block)
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 719
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 720 erase_render_results
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 721 forget_variables_added_to_assigns
722 reset_variables_added_to_assigns
723
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 724 result
725 end
726
dfd953ea » dhh 2006-03-27 Fixed docs 727 def render_action(action_name, status = nil, with_layout = true) #:nodoc:
8896efd4 » dhh 2006-02-18 Ensure backwards compatibil... 728 template = default_template_name(action_name.to_s)
62fe5bbf » Marcel Molina 2005-11-18 Make rjs templates always i... 729 if with_layout && !template_exempt_from_layout?(template)
730 render_with_layout(template, status)
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 731 else
62fe5bbf » Marcel Molina 2005-11-18 Make rjs templates always i... 732 render_without_layout(template, status)
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 733 end
734 end
735
dfd953ea » dhh 2006-03-27 Fixed docs 736 def render_file(template_path, status = nil, use_full_path = false, locals = {}) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 737 add_variables_to_assigns
b6e7cc63 » dhh 2006-03-27 Spell existence properly (c... 738 assert_existence_of_template_file(template_path) if use_full_path
d87618d2 » dhh 2005-09-28 Log request method and resp... 739 logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger
bea737eb » sstephenson 2005-11-22 Make ActionController's ren... 740 render_text(@template.render_file(template_path, use_full_path, locals), status)
41ea6963 » dhh 2005-02-23 Added Base#render_to_string... 741 end
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 742
dfd953ea » dhh 2006-03-27 Fixed docs 743 def render_template(template, status = nil, type = :rhtml, local_assigns = {}) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 744 add_variables_to_assigns
43b6a74f » dhh 2005-10-13 Added :locals support for r... 745 render_text(@template.render_template(type, template, nil, local_assigns), status)
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 746 end
747
dfd953ea » dhh 2006-03-27 Fixed docs 748 def render_text(text = nil, status = nil) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 749 @performed_render = true
750 @response.headers['Status'] = (status || DEFAULT_RENDER_STATUS_CODE).to_s
751 @response.body = text
752 end
753
dfd953ea » dhh 2006-03-27 Fixed docs 754 def render_javascript(javascript, status = nil) #:nodoc:
1c16649b » dhh 2006-03-10 Added better support for us... 755 @response.headers['Content-Type'] = 'text/javascript; charset=UTF-8'
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 756 render_text(javascript, status)
757 end
758
dfd953ea » dhh 2006-03-27 Fixed docs 759 def render_xml(xml, status = nil) #:nodoc:
97dd9a91 » dhh 2006-03-12 Always use application/xml,... 760 @response.headers['Content-Type'] = 'application/xml'
1c16649b » dhh 2006-03-10 Added better support for us... 761 render_text(xml, status)
762 end
763
dfd953ea » dhh 2006-03-27 Fixed docs 764 def render_nothing(status = nil) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 765 render_text(' ', status)
766 end
767
dfd953ea » dhh 2006-03-27 Fixed docs 768 def render_partial(partial_path = default_template_name, object = nil, local_assigns = nil, status = nil) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 769 add_variables_to_assigns
770 render_text(@template.render_partial(partial_path, object, local_assigns), status)
771 end
772
dfd953ea » dhh 2006-03-27 Fixed docs 773 def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = nil, status = nil) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 774 add_variables_to_assigns
775 render_text(@template.render_partial_collection(partial_name, collection, partial_spacer_template, local_assigns), status)
776 end
777
dfd953ea » dhh 2006-03-27 Fixed docs 778 def render_with_layout(template_name = default_template_name, status = nil, layout = nil) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 779 render_with_a_layout(template_name, status, layout)
780 end
781
dfd953ea » dhh 2006-03-27 Fixed docs 782 def render_without_layout(template_name = default_template_name, status = nil) #:nodoc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 783 render_with_no_layout(template_name, status)
784 end
785
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 786
62ed6950 » dhh 2005-06-28 Added support for upload pr... 787 # Clears the rendered results, allowing for another render to be performed.
dfd953ea » dhh 2006-03-27 Fixed docs 788 def erase_render_results #:nodoc:
dab360e1 » dhh 2005-05-21 Added DoubleRenderError exc... 789 @response.body = nil
790 @performed_render = false
791 end
9c52a412 » sstephenson 2006-01-23 Add the ability to call Jav... 792
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 793 # Clears the redirected results from the headers, resets the status to 200 and returns
62ed6950 » dhh 2005-06-28 Added support for upload pr... 794 # the URL that was used to redirect or nil if there was no redirected URL
795 # Note that +redirect_to+ will change the body of the response to indicate a redirection.
796 # The response body is not reset here, see +erase_render_results+
dfd953ea » dhh 2006-03-27 Fixed docs 797 def erase_redirect_results #:nodoc:
62ed6950 » dhh 2005-06-28 Added support for upload pr... 798 @performed_redirect = false
799 response.redirected_to = nil
800 response.redirected_to_method_params = nil
801 response.headers['Status'] = DEFAULT_RENDER_STATUS_CODE
802 response.headers.delete('location')
803 end
804
e9cfe955 » dhh 2005-07-25 Add a catch-all eraser 805 # Erase both render and redirect results
dfd953ea » dhh 2006-03-27 Fixed docs 806 def erase_results #:nodoc:
e9cfe955 » dhh 2005-07-25 Add a catch-all eraser 807 erase_render_results
808 erase_redirect_results
809 end
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 810
dfd953ea » dhh 2006-03-27 Fixed docs 811 def rewrite_options(options) #:nodoc:
db045dbb » dhh 2004-11-23 Initial 812 if defaults = default_url_options(options)
813 defaults.merge(options)
814 else
815 options
816 end
817 end
818
819 # Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in
820 # the form of a hash, just like the one you would use for url_for directly. Example:
821 #
822 # def default_url_options(options)
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 823 # { :project => @project.active? ? @project.url_name : "unknown" }
db045dbb » dhh 2004-11-23 Initial 824 # end
825 #
826 # As you can infer from the example, this is mostly useful for situations where you want to centralize dynamic decisions about the
827 # urls as they stem from the business domain. Please note that any individual url_for call can always override the defaults set
828 # by this method.
829 def default_url_options(options) #:doc:
830 end
831
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 832 # Redirects the browser to the target specified in +options+. This parameter can take one of three forms:
833 #
834 # * <tt>Hash</tt>: The URL will be generated by calling url_for with the +options+.
835 # * <tt>String starting with protocol:// (like http://)</tt>: Is passed straight through as the target for redirection.
01cfd2b0 » dhh 2005-11-07 Fix docs (closes #2725) 836 # * <tt>String not containing a protocol</tt>: The current protocol and host is prepended to the string.
4f754985 » dhh 2005-11-02 Added redirect_to :back as ... 837 # * <tt>:back</tt>: Back to the page that issued the request. Useful for forms that are triggered from multiple places.
838 # Short-hand for redirect_to(request.env["HTTP_REFERER"])
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 839 #
840 # Examples:
841 # redirect_to :action => "show", :id => 5
842 # redirect_to "http://www.rubyonrails.org"
843 # redirect_to "/images/screenshot.jpg"
4f754985 » dhh 2005-11-02 Added redirect_to :back as ... 844 # redirect_to :back
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 845 #
846 # The redirection happens as a "302 Moved" header.
21142201 » Marcel Molina 2006-04-24 Add documentation for redir... 847 #
848 # When using <tt>redirect_to :back</tt>, if there is no referrer,
849 # RedirectBackError will be raised. You may specify some fallback
850 # behavior for this case by rescueing RedirectBackError.
db045dbb » dhh 2004-11-23 Initial 851 def redirect_to(options = {}, *parameters_for_method_reference) #:doc:
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 852 case options
853 when %r{^\w+://.*}
82668678 » jamis 2005-07-09 Improved error message for ... 854 raise DoubleRenderError if performed?
76540822 » jeremy 2006-02-09 Major components cleanup an... 855 logger.info("Redirected to #{options}") if logger
060ecf1a » dhh 2005-05-22 Set redirected_to proper 856 response.redirect(options)
857 response.redirected_to = options
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 858 @performed_redirect = true
859
860 when String
861 redirect_to(request.protocol + request.host_with_port + options)
4f754985 » dhh 2005-11-02 Added redirect_to :back as ... 862
863 when :back
ea30f735 » dhh 2006-01-21 Raise a RedirectBackError i... 864 request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : raise(RedirectBackError)
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 865
866 else
867 if parameters_for_method_reference.empty?
868 redirect_to(url_for(options))
060ecf1a » dhh 2005-05-22 Set redirected_to proper 869 response.redirected_to = options
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 870 else
871 redirect_to(url_for(options, *parameters_for_method_reference))
060ecf1a » dhh 2005-05-22 Set redirected_to proper 872 response.redirected_to, response.redirected_to_method_params = options, parameters_for_method_reference
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 873 end
db045dbb » dhh 2004-11-23 Initial 874 end
875 end
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 876
877 # Sets a HTTP 1.1 Cache-Control header. Defaults to issuing a "private" instruction, so that
878 # intermediate caches shouldn't cache the response.
879 #
880 # Examples:
881 # expires_in 20.minutes
882 # expires_in 3.hours, :private => false
883 # expires in 3.hours, 'max-stale' => 5.hours, :private => nil, :public => true
884 #
885 # This method will overwrite an existing Cache-Control header.
886 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities.
d1725929 » dhh 2005-07-23 Added test for template to ... 887 def expires_in(seconds, options = {}) #:doc:
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 888 cache_options = { 'max-age' => seconds, 'private' => true }.symbolize_keys.merge!(options.symbolize_keys)
889 cache_options.delete_if { |k,v| v.nil? or v == false }
890 cache_control = cache_options.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
891 @response.headers["Cache-Control"] = cache_control.join(', ')
892 end
893
894 # Sets a HTTP 1.1 Cache-Control header of "no-cache" so no caching should occur by the browser or
895 # intermediate caches (like caching proxy servers).
d1725929 » dhh 2005-07-23 Added test for template to ... 896 def expires_now #:doc:
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 897 @response.headers["Cache-Control"] = "no-cache"
898 end
db045dbb » dhh 2004-11-23 Initial 899
098fa943 » dhh 2005-02-07 Fixed documentation snafus ... 900 # Resets the session by clearing out all the objects stored within and initializing a new session object.
db045dbb » dhh 2004-11-23 Initial 901 def reset_session #:doc:
902 @request.reset_session
903 @session = @request.session
904 @response.session = @session
905 end
906
907 private
bf92aacf » seckar 2005-09-08 Avoid extending view instan... 908 def self.view_class
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 909 @view_class ||=
bf92aacf » seckar 2005-09-08 Avoid extending view instan... 910 # create a new class based on the default template class and include helper methods
eedd9d76 » dhh 2006-03-19 Updated Kernel#returning fo... 911 returning Class.new(ActionView::Base) do |view_class|
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 912 view_class.send(:include, master_helper_module)
913 end
bf92aacf » seckar 2005-09-08 Avoid extending view instan... 914 end
915
916 def self.view_root
917 @view_root ||= template_root
918 end
919
920 def initialize_template_class(response)
921 raise "You must assign a template class through ActionController.template_class= before processing a request" unless @@template_class
db045dbb » dhh 2004-11-23 Initial 922
bf92aacf » seckar 2005-09-08 Avoid extending view instan... 923 response.template = self.class.view_class.new(self.class.view_root, {}, self)
02594910 » rubyist 2005-10-30 Fix problem where redirecti... 924 response.redirected_to = nil
db045dbb » dhh 2004-11-23 Initial 925 @performed_render = @performed_redirect = false
926 end
927
928 def assign_shortcuts(request, response)
929 @request, @params, @cookies = request, request.parameters, request.cookies
930
931 @response = response
932 @response.session = request.session
933
934 @session = @response.session
935 @template = @response.template
76540822 » jeremy 2006-02-09 Major components cleanup an... 936 @assigns = @response.template.assigns
937
db045dbb » dhh 2004-11-23 Initial 938 @headers = @response.headers
939 end
940
941 def initialize_current_url
b1999be5 » dhh 2005-02-14 A hopefully more successful... 942 @url = UrlRewriter.new(@request, @params.clone())
db045dbb » dhh 2004-11-23 Initial 943 end
944
945 def log_processing
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 946 if logger
947 logger.info "\n\nProcessing #{controller_class_name}\##{action_name} (for #{request_origin}) [#{request.method.to_s.upcase}]"
948 logger.info " Session ID: #{@session.session_id}" if @session and @session.respond_to?(:session_id)
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 949 logger.info " Parameters: #{respond_to?(:filter_parameters) ? filter_parameters(@params).inspect : @params.inspect}"
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 950 end
db045dbb » dhh 2004-11-23 Initial 951 end
952
953 def perform_action
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 954 if self.class.action_methods.include?(action_name) || self.class.action_methods.include?('method_missing')
db045dbb » dhh 2004-11-23 Initial 955 send(action_name)
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 956 render unless performed?
db045dbb » dhh 2004-11-23 Initial 957 elsif template_exists? && template_public?
958 render
959 else
960 raise UnknownAction, "No action responded to #{action_name}", caller
961 end
962 end
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 963
964 def performed?
965 @performed_render || @performed_redirect
966 end
db045dbb » dhh 2004-11-23 Initial 967
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 968 def assign_names
969 @action_name = (params['action'] || 'index')
970 end
971
db045dbb » dhh 2004-11-23 Initial 972 def action_methods
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 973 self.class.action_methods
db045dbb » dhh 2004-11-23 Initial 974 end
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 975
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 976 def self.action_methods
22d9bad8 » jeremy 2005-10-20 Expose the session model ba... 977 @action_methods ||= Set.new(public_instance_methods - hidden_actions)
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 978 end
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 979
db045dbb » dhh 2004-11-23 Initial 980 def add_variables_to_assigns
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 981 unless @variables_added
982 add_instance_variables_to_assigns
983 add_class_variables_to_assigns if view_controller_internals
984 @variables_added = true
985 end
db045dbb » dhh 2004-11-23 Initial 986 end
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 987
988 def forget_variables_added_to_assigns
989 @variables_added = nil
990 end
991
992 def reset_variables_added_to_assigns
993 @template.instance_variable_set("@assigns_added", nil)
994 end
db045dbb » dhh 2004-11-23 Initial 995
996 def add_instance_variables_to_assigns
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 997 @@protected_variables_cache ||= protected_instance_variables.inject({}) { |h, k| h[k] = true; h }
db045dbb » dhh 2004-11-23 Initial 998 instance_variables.each do |var|
fa158ff0 » jeremy 2005-07-04 r2829@asus: jeremy | 2005... 999 next if @@protected_variables_cache.include?(var)
db045dbb » dhh 2004-11-23 Initial 1000 @assigns[var[1..-1]] = instance_variable_get(var)
1001 end
1002 end
1003
1004 def add_class_variables_to_assigns
1005 %w( template_root logger template_class ignore_missing_templates ).each do |cvar|
1006 @assigns[cvar] = self.send(cvar)
1007 end
1008 end
1009
1010 def protected_instance_variables
1011 if view_controller_internals
1012 [ "@assigns", "@performed_redirect", "@performed_render" ]
1013 else
76540822 » jeremy 2006-02-09 Major components cleanup an... 1014 [ "@assigns", "@performed_redirect", "@performed_render", "@request", "@response", "@session", "@cookies", "@template", "@request_origin", "@parent_controller" ]
db045dbb » dhh 2004-11-23 Initial 1015 end
1016 end
1017
1018 def request_origin
76540822 » jeremy 2006-02-09 Major components cleanup an... 1019 # this *needs* to be cached!
1020 # otherwise you'd get different results if calling it more than once
1021 @request_origin ||= "#{@request.remote_ip} at #{Time.now.to_s(:db)}"
db045dbb » dhh 2004-11-23 Initial 1022 end
1023
977b4be2 » dhh 2005-07-05 Changed logging of SQL stat... 1024 def complete_request_uri
76540822 » jeremy 2006-02-09 Major components cleanup an... 1025 "#{@request.protocol}#{@request.host}#{@request.request_uri}"
977b4be2 » dhh 2005-07-05 Changed logging of SQL stat... 1026 end
1027
db045dbb » dhh 2004-11-23 Initial 1028 def close_session
1029 @session.close unless @session.nil? || Hash === @session
1030 end
1031
1032 def template_exists?(template_name = default_template_name)
1033 @template.file_exists?(template_name)
1034 end
1035
1036 def template_public?(template_name = default_template_name)
1037 @template.file_public?(template_name)
1038 end
1039
62fe5bbf » Marcel Molina 2005-11-18 Make rjs templates always i... 1040 def template_exempt_from_layout?(template_name = default_template_name)
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1041 template_name =~ /\.rjs$/ || (@template.pick_template_extension(template_name) == :rjs rescue false)
62fe5bbf » Marcel Molina 2005-11-18 Make rjs templates always i... 1042 end
1043
b6e7cc63 » dhh 2006-03-27 Spell existence properly (c... 1044 def assert_existence_of_template_file(template_name)
db045dbb » dhh 2004-11-23 Initial 1045 unless template_exists?(template_name) || ignore_missing_templates
1046 full_template_path = @template.send(:full_template_path, template_name, 'rhtml')
1047 template_type = (template_name =~ /layouts/i) ? 'layout' : 'template'
1048 raise(MissingTemplate, "Missing #{template_type} #{full_template_path}")
1049 end
1050 end
1051
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1052 def default_template_name(action_name = self.action_name)
1053 if action_name
1054 action_name = action_name.to_s
1055 if action_name.include?('/') && template_path_includes_controller?(action_name)
1056 action_name = strip_out_controller(action_name)
1057 end
233208b8 » Marcel Molina 2005-12-19 Don't try to strip out the ... 1058 end
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1059 "#{self.class.controller_path}/#{action_name}"
db045dbb » dhh 2004-11-23 Initial 1060 end
39766a9b » Marcel Molina 2005-12-16 Don't include a layout when... 1061
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1062 def strip_out_controller(path)
1063 path.split('/', 2).last
39766a9b » Marcel Molina 2005-12-16 Don't include a layout when... 1064 end
1065
1066 def template_path_includes_controller?(path)
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1067 self.class.controller_path.split('/')[-1] == path.split('/')[0]
39766a9b » Marcel Molina 2005-12-16 Don't include a layout when... 1068 end
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 1069
1070 def process_cleanup
1071 close_session
1072 end
db045dbb » dhh 2004-11-23 Initial 1073 end
1074 end