public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
dhh (author)
Sun Dec 16 17:22:11 -0800 2007
commit  c8da518bbfedc2a06b1d96912ddae00e57f21748
tree    61bc174fe871cfccf8d8af39e664959918896b12
parent  7af985f74fdaead950986038570f4d00befe7fc0
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'
b2ede64a » jamis 2006-09-28 Add ActionController::Base#... 7 require 'action_controller/status_codes'
df79e135 » dhh 2005-01-08 Added first stab at page an... 8 require 'drb'
22d9bad8 » jeremy 2005-10-20 Expose the session model ba... 9 require 'set'
db045dbb » dhh 2004-11-23 Initial 10
11 module ActionController #:nodoc:
12 class ActionControllerError < StandardError #:nodoc:
13 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 14
db045dbb » dhh 2004-11-23 Initial 15 class SessionRestoreError < ActionControllerError #:nodoc:
16 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 17
db045dbb » dhh 2004-11-23 Initial 18 class MissingTemplate < ActionControllerError #:nodoc:
19 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 20
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 21 class RenderError < ActionControllerError #:nodoc:
22 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 23
8e56f5ea » dhh 2005-06-24 Improved performance of Rou... 24 class RoutingError < ActionControllerError #:nodoc:
b1999be5 » dhh 2005-02-14 A hopefully more successful... 25 attr_reader :failures
26 def initialize(message, failures=[])
27 super(message)
28 @failures = failures
29 end
30 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 31
dcaa074a » jeremy 2007-05-26 Routing: respond with 405 M... 32 class MethodNotAllowed < ActionControllerError #:nodoc:
33 attr_reader :allowed_methods
34
35 def initialize(*allowed_methods)
36 super("Only #{allowed_methods.to_sentence} requests are allowed.")
37 @allowed_methods = allowed_methods
38 end
39
40 def allowed_methods_header
41 allowed_methods.map { |method_symbol| method_symbol.to_s.upcase } * ', '
42 end
43
44 def handle_response!(response)
45 response.headers['Allow'] ||= allowed_methods_header
46 end
47 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 48
dcaa074a » jeremy 2007-05-26 Routing: respond with 405 M... 49 class NotImplemented < MethodNotAllowed #:nodoc:
50 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 51
b1999be5 » dhh 2005-02-14 A hopefully more successful... 52 class UnknownController < ActionControllerError #:nodoc:
53 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 54
db045dbb » dhh 2004-11-23 Initial 55 class UnknownAction < ActionControllerError #:nodoc:
56 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 57
250a570c » dhh 2005-01-01 Added class declaration for... 58 class MissingFile < ActionControllerError #:nodoc:
59 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 60
ff063d70 » Tobias Lütke 2006-08-29 respond_to .html now always... 61 class RenderError < ActionControllerError #:nodoc:
62 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 63
48fd667b » Marcel Molina 2005-10-15 Raise an exception if an at... 64 class SessionOverflowError < ActionControllerError #:nodoc:
65 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... 66
67 def initialize(message = nil)
68 super(message || DEFAULT_MESSAGE)
69 end
48fd667b » Marcel Molina 2005-10-15 Raise an exception if an at... 70 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 71
dab360e1 » dhh 2005-05-21 Added DoubleRenderError exc... 72 class DoubleRenderError < ActionControllerError #:nodoc:
37b874bb » technoweenie 2007-12-08 Fix DoubleRenderError messa... 73 DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most 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\"."
82668678 » jamis 2005-07-09 Improved error message for ... 74
0abaf3a2 » jeremy 2005-11-08 CGI::Session::ActiveRecordS... 75 def initialize(message = nil)
82668678 » jamis 2005-07-09 Improved error message for ... 76 super(message || DEFAULT_MESSAGE)
77 end
dab360e1 » dhh 2005-05-21 Added DoubleRenderError exc... 78 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 79
ea30f735 » dhh 2006-01-21 Raise a RedirectBackError i... 80 class RedirectBackError < ActionControllerError #:nodoc:
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 81 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"].'
82
ea30f735 » dhh 2006-01-21 Raise a RedirectBackError i... 83 def initialize(message = nil)
84 super(message || DEFAULT_MESSAGE)
85 end
86 end
db045dbb » dhh 2004-11-23 Initial 87
0a9bc591 » technoweenie 2007-11-28 Raise UnknownHttpMethod exc... 88 class UnknownHttpMethod < ActionControllerError #:nodoc:
89 end
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 90
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 91 # Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 92 # on request and then either render a template or redirect to another action. An action is defined as a public method
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 93 # on the controller, which will automatically be made accessible to the web-server through Rails Routes.
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 94 #
95 # A sample controller could look like this:
db045dbb » dhh 2004-11-23 Initial 96 #
97 # class GuestBookController < ActionController::Base
98 # def index
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 99 # @entries = Entry.find(:all)
db045dbb » dhh 2004-11-23 Initial 100 # end
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 101 #
db045dbb » dhh 2004-11-23 Initial 102 # def sign
1c057b72 » jamis 2005-10-16 Update/clean up AP document... 103 # Entry.create(params[:entry])
db045dbb » dhh 2004-11-23 Initial 104 # redirect_to :action => "index"
105 # end
106 # end
107 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 108 # Actions, by default, render a template in the <tt>app/views</tt> directory corresponding to the name of the controller and action
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 109 # after executing code in the action. For example, the +index+ action of the +GuestBookController+ would render the
e1056530 » dhh 2007-02-20 Added .erb and .builder as ... 110 # template <tt>app/views/guestbook/index.erb</tt> by default after populating the <tt>@entries</tt> instance variable.
db045dbb » dhh 2004-11-23 Initial 111 #
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 112 # Unlike index, the sign action will not render a template. After performing its main purpose (creating a
113 # new entry in the guest book), it initiates a redirect instead. This redirect works by returning an external
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 114 # "302 Moved" HTTP response that takes the user to the index action.
db045dbb » dhh 2004-11-23 Initial 115 #
116 # The index and sign represent the two basic action archetypes used in Action Controllers. Get-and-show and do-and-redirect.
117 # Most actions are variations of these themes.
118 #
119 # == Requests
120 #
121 # Requests are processed by the Action Controller framework by extracting the value of the "action" key in the request parameters.
122 # This value should hold the name of the action to be performed. Once the action has been identified, the remaining
123 # request parameters, the session (if one is available), and the full request with all the http headers are made available to
124 # the action through instance variables. Then the action is performed.
125 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 126 # The full request object is available with the request accessor and is primarily used to query for http headers. These queries
127 # are made by accessing the environment hash, like this:
db045dbb » dhh 2004-11-23 Initial 128 #
0cd883e1 » jeremy 2006-05-17 Change the request.env exam... 129 # def server_ip
130 # location = request.env["SERVER_ADDR"]
131 # render :text => "This server hosted at #{location}"
db045dbb » dhh 2004-11-23 Initial 132 # end
133 #
134 # == Parameters
135 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 136 # All request parameters, whether they come from a GET or POST request, or from the URL, are available through the params method
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 137 # which returns a hash. For example, an action that was performed through <tt>/weblog/list?category=All&limit=5</tt> will include
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 138 # <tt>{ "category" => "All", "limit" => 5 }</tt> in params.
db045dbb » dhh 2004-11-23 Initial 139 #
140 # It's also possible to construct multi-dimensional parameter hashes by specifying keys using brackets, such as:
141 #
142 # <input type="text" name="post[name]" value="david">
143 # <input type="text" name="post[address]" value="hyacintvej">
144 #
e4efcfd4 » dhh 2005-02-23 Updated documentation 145 # A request stemming from a form holding these inputs will include <tt>{ "post" => { "name" => "david", "address" => "hyacintvej" } }</tt>.
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 146 # If the address input had been named "post[address][street]", the params would have included
e4efcfd4 » dhh 2005-02-23 Updated documentation 147 # <tt>{ "post" => { "address" => { "street" => "hyacintvej" } } }</tt>. There's no limit to the depth of the nesting.
db045dbb » dhh 2004-11-23 Initial 148 #
149 # == Sessions
150 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 151 # 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 152 # 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
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 153 # 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
db045dbb » dhh 2004-11-23 Initial 154 # they could be changed unknowingly. It's usually too much work to keep it all synchronized -- something databases already excel at.
155 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 156 # You can place objects in the session by using the <tt>session</tt> method, which accesses a hash:
db045dbb » dhh 2004-11-23 Initial 157 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 158 # session[:person] = Person.authenticate(user_name, password)
db045dbb » dhh 2004-11-23 Initial 159 #
160 # And retrieved again through the same hash:
161 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 162 # Hello #{session[:person]}
db045dbb » dhh 2004-11-23 Initial 163 #
46c4fd41 » jamis 2005-09-01 ActionController documentat... 164 # 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... 165 # remove the entire session with reset_session.
166 #
2af36bbb » dhh 2007-12-05 Fix typos (closes #10378) 167 # Sessions are stored in a browser cookie that's cryptographically signed, but unencrypted, by default. This prevents
96add62e » jeremy 2007-11-21 Document that the cookie st... 168 # the user from tampering with the session but also allows him to see its contents.
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 169 #
96add62e » jeremy 2007-11-21 Document that the cookie st... 170 # Do not put secret information in session!
171 #
172 # Other options for session storage are:
173 #
174 # ActiveRecordStore: sessions are stored in your database, which works better than PStore with multiple app servers and,
175 # unlike CookieStore, hides your session contents from the user. To use ActiveRecordStore, set
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 176 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 177 # config.action_controller.session_store = :active_record_store
178 #
179 # in your <tt>environment.rb</tt> and run <tt>rake db:sessions:create</tt>.
180 #
96add62e » jeremy 2007-11-21 Document that the cookie st... 181 # MemCacheStore: sessions are stored as entries in your memcached cache. Set the session store type in <tt>environment.rb</tt>:
182 #
183 # config.action_controller.session_store = :mem_cache_store
184 #
185 # This assumes that memcached has been installed and configured properly. See the MemCacheStore docs for more information.
186 #
db045dbb » dhh 2004-11-23 Initial 187 # == Responses
188 #
189 # 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... 190 # object is generated automatically through the use of renders and redirects and requires no user intervention.
db045dbb » dhh 2004-11-23 Initial 191 #
192 # == Renders
193 #
194 # Action Controller sends content to the user by using one of five rendering methods. The most versatile and common is the rendering
195 # of a template. Included in the Action Pack is the Action View, which enables rendering of ERb templates. It's automatically configured.
196 # The controller passes objects to the view by assigning instance variables:
197 #
198 # def show
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 199 # @post = Post.find(params[:id])
db045dbb » dhh 2004-11-23 Initial 200 # end
201 #
202 # Which are then automatically available to the view:
203 #
204 # Title: <%= @post.title %>
205 #
206 # You don't have to rely on the automated rendering. Especially actions that could result in the rendering of different templates will use
207 # the manual rendering methods:
208 #
209 # def search
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 210 # @results = Search.find(params[:query])
db045dbb » dhh 2004-11-23 Initial 211 # case @results
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 212 # when 0 then render :action => "no_results"
213 # when 1 then render :action => "show"
214 # when 2..10 then render :action => "show_many"
db045dbb » dhh 2004-11-23 Initial 215 # end
216 # end
217 #
218 # Read more about writing ERb and Builder templates in link:classes/ActionView/Base.html.
219 #
220 # == Redirects
221 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 222 # 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,
223 # 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)
224 # a <tt>show</tt> action that we'll assume has already been created. The code might look like this:
225 #
226 # def create
227 # @entry = Entry.new(params[:entry])
228 # if @entry.save
229 # # The entry was saved correctly, redirect to show
230 # redirect_to :action => 'show', :id => @entry.id
231 # else
232 # # things didn't go so well, do something else
233 # end
234 # end
db045dbb » dhh 2004-11-23 Initial 235 #
dd5397a5 » Marcel Molina 2006-04-27 ActionController::Base Summ... 236 # 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 237 #
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 238 # == Calling multiple redirects or renders
239 #
12d8d48b » NZKoz 2007-10-24 Refactor the default render... 240 # An action may contain only a single render or a single redirect. Attempting to try to do either again will result in a DoubleRenderError:
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 241 #
242 # def do_something
243 # redirect_to :action => "elsewhere"
14e7c7c2 » dhh 2005-07-09 Better documentation for Ca... 244 # render :action => "overthere" # raises DoubleRenderError
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 245 # end
246 #
14e7c7c2 » dhh 2005-07-09 Better documentation for Ca... 247 # 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... 248 #
14e7c7c2 » dhh 2005-07-09 Better documentation for Ca... 249 # def do_something
250 # redirect_to(:action => "elsewhere") and return if monkeys.nil?
251 # render :action => "overthere" # won't be called unless monkeys is nil
46c4fd41 » jamis 2005-09-01 ActionController documentat... 252 # end
253 #
db045dbb » dhh 2004-11-23 Initial 254 class Base
255 DEFAULT_RENDER_STATUS_CODE = "200 OK"
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 256
b2ede64a » jamis 2006-09-28 Add ActionController::Base#... 257 include StatusCodes
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 258
db045dbb » dhh 2004-11-23 Initial 259 # Determines whether the view has access to controller internals @request, @response, @session, and @template.
260 # By default, it does.
261 @@view_controller_internals = true
262 cattr_accessor :view_controller_internals
263
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 264 # Protected instance variable cache
265 @@protected_variables_cache = nil
266 cattr_accessor :protected_variables_cache
267
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 268 # Prepends all the URL-generating helpers from AssetHelper. This makes it possible to easily move javascripts, stylesheets,
269 # and images to a dedicated asset server away from the main web server. Example:
c971c248 » dhh 2005-05-05 Changed RAILS_ASSET_HOST to... 270 # ActionController::Base.asset_host = "http://assets.example.com"
271 @@asset_host = ""
272 cattr_accessor :asset_host
273
db045dbb » dhh 2004-11-23 Initial 274 # All requests are considered local by default, so everyone will be exposed to detailed debugging screens on errors.
275 # When the application is ready to go public, this should be set to false, and the protected method <tt>local_request?</tt>
276 # should instead be implemented in the controller to determine when debugging screens should be shown.
277 @@consider_all_requests_local = true
278 cattr_accessor :consider_all_requests_local
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 279
b1999be5 » dhh 2005-02-14 A hopefully more successful... 280 # Enable or disable the collection of failure information for RoutingErrors.
281 # This information can be extremely useful when tweaking custom routes, but is
282 # pointless once routes have been tested and verified.
283 @@debug_routes = true
284 cattr_accessor :debug_routes
db045dbb » dhh 2004-11-23 Initial 285
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 286 # 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... 287 # around the performance of each action. Action Pack and Active Record are by default thread-safe, but many applications
288 # may not be. Turned off by default.
289 @@allow_concurrency = false
290 cattr_accessor :allow_concurrency
291
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 292 # Modern REST web services often need to submit complex data to the web application.
fc901e6f » dhh 2006-09-04 Doc fix (closes #6023) 293 # The param_parsers hash lets you register handlers which will process the http body and add parameters to the
09095c72 » Marcel Molina 2006-04-25 Remove all remaining refere... 294 # <tt>params</tt> hash. These handlers are invoked for post and put requests.
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 295 #
2af36bbb » dhh 2007-12-05 Fix typos (closes #10378) 296 # By default application/xml is enabled. A XmlSimple class with the same param name as the root will be instantiated
09095c72 » Marcel Molina 2006-04-25 Remove all remaining refere... 297 # 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... 298 # action serve both regular forms and web service requests.
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 299 #
dc4b5cff » dhh 2006-03-06 XmlSimple _should_ be the d... 300 # Example of doing your own parser for a custom content type:
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 301 #
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 302 # ActionController::Base.param_parsers[Mime::Type.lookup('application/atom+xml')] = Proc.new do |data|
303 # node = REXML::Document.new(post)
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 304 # { node.root.name => node.root }
305 # end
306 #
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 307 # Note: Up until release 1.1 of Rails, Action Controller would default to using XmlSimple configured to discard the
dc4b5cff » dhh 2006-03-06 XmlSimple _should_ be the d... 308 # root node for such requests. The new default is to keep the root, such that "<r><name>David</name></r>" results
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 309 # 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... 310 # re-register XmlSimple as application/xml handler ike this:
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 311 #
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 312 # ActionController::Base.param_parsers[Mime::XML] =
dc4b5cff » dhh 2006-03-06 XmlSimple _should_ be the d... 313 # Proc.new { |data| XmlSimple.xml_in(data, 'ForceArray' => false) }
62dc792a » dhh 2006-04-04 CHANGED DEFAULT: Don't pars... 314 #
315 # A YAML parser is also available and can be turned on with:
316 #
317 # ActionController::Base.param_parsers[Mime::YAML] = :yaml
d2ed32d5 » jeremy 2007-05-17 Parse url-encoded and multi... 318 @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
319 Mime::URL_ENCODED_FORM => :url_encoded_form,
320 Mime::XML => :xml_simple }
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 321 cattr_accessor :param_parsers
03d37a2d » Tobias Lütke 2006-03-05 Added new infrastructure su... 322
2caf4d5a » dhh 2006-09-17 Added proper getters and se... 323 # Controls the default charset for all renders.
324 @@default_charset = "utf-8"
325 cattr_accessor :default_charset
69b0e5c4 » technoweenie 2007-02-04 Allow Controllers to have m... 326
db045dbb » dhh 2004-11-23 Initial 327 # The logger is used for generating information on the action run-time (including benchmarking) if available.
328 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
329 cattr_accessor :logger
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 330
db045dbb » dhh 2004-11-23 Initial 331 # Determines which template class should be used by ActionController.
332 cattr_accessor :template_class
333
334 # Turn on +ignore_missing_templates+ if you want to unit test actions without making the associated templates.
335 cattr_accessor :ignore_missing_templates
336
68d68505 » Tobias Lütke 2007-09-06 Remove deprecated named rou... 337 # Controls the resource action separator
338 @@resource_action_separator = "/"
339 cattr_accessor :resource_action_separator
4e3ed5bc » technoweenie 2007-09-22 Merge csrf_killer plugin in... 340
c6190038 » technoweenie 2007-09-23 Rename some RequestForgeryP... 341 # Sets the token parameter name for RequestForgery. Calling #protect_from_forgery sets it to :authenticity_token by default
4e3ed5bc » technoweenie 2007-09-22 Merge csrf_killer plugin in... 342 cattr_accessor :request_forgery_protection_token
904df818 » technoweenie 2007-10-02 Move ActionController::Rout... 343
344 # Indicates whether or not optimise the generated named
345 # route helper methods
346 cattr_accessor :optimise_named_routes
347 self.optimise_named_routes = true
348
5edc81dc » technoweenie 2007-09-28 Allow ability to disable re... 349 # Controls whether request forgergy protection is turned on or not. Turned off by default only in test mode.
350 class_inheritable_accessor :allow_forgery_protection
351 self.allow_forgery_protection = true
68d68505 » Tobias Lütke 2007-09-06 Remove deprecated named rou... 352
db045dbb » dhh 2004-11-23 Initial 353 # 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... 354 # <tt>request.env["REQUEST_URI"]</tt>.
d7674637 » jeremy 2006-09-29 Deprecation: @request will ... 355 attr_internal :request
c40b1a4a » jeremy 2006-08-07 Deprecate direct usage of @... 356
46c4fd41 » jamis 2005-09-01 ActionController documentat... 357 # 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 358 # to get the post_id. No type casts are made, so all values are returned as strings.
c40b1a4a » jeremy 2006-08-07 Deprecate direct usage of @... 359 attr_internal :params
360
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 361 # 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... 362 # <tt>response.headers["Cache-Control"] = "no-cache"</tt>. Can also be used to access the final body HTML after a template
363 # 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 364 # such as a OutputCompressionFilter.
d15d15b2 » jeremy 2006-09-29 Deprecate @response 365 attr_internal :response
c40b1a4a » jeremy 2006-08-07 Deprecate direct usage of @... 366
6ee06ebe » dhh 2005-04-16 Changed render_partial to t... 367 # 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) 368 # key. The session will hold any type of object as values, but the key should be a string or symbol.
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 369 attr_internal :session
370
6ee06ebe » dhh 2005-04-16 Changed render_partial to t... 371 # 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 372 # directive. Values should always be specified as strings.
d7b5b44a » jeremy 2006-09-29 Deprecate @headers 373 attr_internal :headers
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 374
db045dbb » dhh 2004-11-23 Initial 375 # Holds the hash of variables that are passed on to the template class to be made available to the view. This hash
376 # is generated by taking a snapshot of all the instance variables in the current scope just before a template is rendered.
377 attr_accessor :assigns
378
c6f819ed » jeremy 2005-07-04 r2827@asus: jeremy | 2005... 379 # Returns the name of the action this controller is processing.
380 attr_accessor :action_name
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 381
35ffc1af » jeremy 2006-09-15 Declare file extensions exe... 382 # Templates that are exempt from layouts
383 @@exempt_from_layout = Set.new([/\.rjs$/])
384
db045dbb » dhh 2004-11-23 Initial 385 class << self
386 # Factory for the standard create, process loop where the controller is discarded after processing.
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 387 def process(request, response) #:nodoc:
388 new.process(request, response)
db045dbb » dhh 2004-11-23 Initial 389 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 390
db045dbb » dhh 2004-11-23 Initial 391 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
392 def controller_class_name
d2b9e39c » jeremy 2005-07-04 r2828@asus: jeremy | 2005... 393 @controller_class_name ||= name.demodulize
db045dbb » dhh 2004-11-23 Initial 394 end
395
396 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
397 def controller_name
d2b9e39c » jeremy 2005-07-04 r2828@asus: jeremy | 2005... 398 @controller_name ||= controller_class_name.sub(/Controller$/, '').underscore
db045dbb » dhh 2004-11-23 Initial 399 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 400
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 401 # 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... 402 def controller_path
8ff44631 » seckar 2006-03-13 Simplify controller_path 403 @controller_path ||= name.gsub(/Controller$/, '').underscore
b1999be5 » dhh 2005-02-14 A hopefully more successful... 404 end
04b8bc1b » dhh 2005-02-17 Fixed that a bunch of metho... 405
406 # Return an array containing the names of public methods that have been marked hidden from the action processor.
407 # By default, all methods defined in ActionController::Base and included modules are hidden.
13779431 » dhh 2006-02-11 The components module shoul... 408 # More methods can be hidden using <tt>hide_actions</tt>.
04b8bc1b » dhh 2005-02-17 Fixed that a bunch of metho... 409 def hidden_actions
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 410 unless read_inheritable_attribute(:hidden_actions)
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 411 write_inheritable_attribute(:hidden_actions, ActionController::Base.public_instance_methods.map(&:to_s))
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 412 end
413
04b8bc1b » dhh 2005-02-17 Fixed that a bunch of metho... 414 read_inheritable_attribute(:hidden_actions)
415 end
416
417 # Hide each of the given methods from being callable as actions.
69f18a7a » dhh 2005-02-23 Keep the singular style, li... 418 def hide_action(*names)
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 419 write_inheritable_attribute(:hidden_actions, hidden_actions | names.map(&:to_s))
12a75736 » dhh 2005-02-20 Added new keyword to specif... 420 end
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 421
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 422 ## View load paths determine the bases from which template references can be made. So a call to
423 ## render("test/template") will be looked up in the view load paths array and the closest match will be
424 ## returned.
425 def view_paths
426 @view_paths || superclass.view_paths
427 end
89b76306 » dhh 2007-09-09 Removed the deprecated Acti... 428
69b0e5c4 » technoweenie 2007-02-04 Allow Controllers to have m... 429 def view_paths=(value)
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 430 @view_paths = value
69b0e5c4 » technoweenie 2007-02-04 Allow Controllers to have m... 431 end
92195e68 » technoweenie 2007-02-04 Fix issue with deprecation ... 432
433 # Adds a view_path to the front of the view_paths array.
434 # If the current class has no view paths, copy them from
87e506da » technoweenie 2007-11-25 Add #prepend_view_path and ... 435 # the superclass. This change will be visible for all future requests.
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 436 #
437 # ArticleController.prepend_view_path("views/default")
438 # ArticleController.prepend_view_path(["views/default", "views/custom"])
439 #
92195e68 » technoweenie 2007-02-04 Fix issue with deprecation ... 440 def prepend_view_path(path)
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 441 @view_paths = superclass.view_paths.dup if @view_paths.nil?
442 view_paths.unshift(*path)
92195e68 » technoweenie 2007-02-04 Fix issue with deprecation ... 443 end
444
445 # Adds a view_path to the end of the view_paths array.
446 # If the current class has no view paths, copy them from
87e506da » technoweenie 2007-11-25 Add #prepend_view_path and ... 447 # the superclass. This change will be visible for all future requests.
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 448 #
449 # ArticleController.append_view_path("views/default")
450 # ArticleController.append_view_path(["views/default", "views/custom"])
451 #
92195e68 » technoweenie 2007-02-04 Fix issue with deprecation ... 452 def append_view_path(path)
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 453 @view_paths = superclass.view_paths.dup if @view_paths.nil?
454 view_paths.push(*path)
92195e68 » technoweenie 2007-02-04 Fix issue with deprecation ... 455 end
456
2af36bbb » dhh 2007-12-05 Fix typos (closes #10378) 457 # Replace sensitive parameter data from the request log.
458 # Filters parameters that have any of the arguments as a substring.
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 459 # Looks in all subhashes of the param hash for keys to filter.
2af36bbb » dhh 2007-12-05 Fix typos (closes #10378) 460 # If a block is given, each key and value of the parameter hash and all
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 461 # subhashes is passed to it, the value or key
462 # can be replaced using String#replace or similar method.
463 #
464 # Examples:
465 # filter_parameter_logging
466 # => Does nothing, just slows the logging process down
467 #
468 # filter_parameter_logging :password
469 # => replaces the value to all keys matching /password/i with "[FILTERED]"
470 #
471 # filter_parameter_logging :foo, "bar"
472 # => replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
473 #
474 # filter_parameter_logging { |k,v| v.reverse! if k =~ /secret/i }
475 # => reverses the value to all keys matching /secret/i
476 #
477 # filter_parameter_logging(:foo, "bar") { |k,v| v.reverse! if k =~ /secret/i }
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 478 # => reverses the value to all keys matching /secret/i, and
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 479 # replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
480 def filter_parameter_logging(*filter_words, &block)
481 parameter_filter = Regexp.new(filter_words.collect{ |s| s.to_s }.join('|'), true) if filter_words.length > 0
482
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 483 define_method(:filter_parameters) do |unfiltered_parameters|
484 filtered_parameters = {}
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 485
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 486 unfiltered_parameters.each do |key, value|
487 if key =~ parameter_filter
488 filtered_parameters[key] = '[FILTERED]'
489 elsif value.is_a?(Hash)
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 490 filtered_parameters[key] = filter_parameters(value)
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 491 elsif block_given?
8ba22a69 » jeremy 2007-05-21 Fix filtered parameter logg... 492 key = key.dup
493 value = value.dup if value
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 494 yield key, value
495 filtered_parameters[key] = value
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 496 else
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 497 filtered_parameters[key] = value
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 498 end
0049bd72 » Marcel Molina 2006-04-29 Update README 499 end
869c41d8 » Marcel Molina 2006-04-29 Revert unintential change t... 500
501 filtered_parameters
47b74e6e » dhh 2006-04-07 Added ActionController.filt... 502 end
503 end
35ffc1af » jeremy 2006-09-15 Declare file extensions exe... 504
505 # Don't render layouts for templates with the given extensions.
506 def exempt_from_layout(*extensions)
507 regexps = extensions.collect do |extension|
508 extension.is_a?(Regexp) ? extension : /\.#{Regexp.escape(extension.to_s)}$/
509 end
510 @@exempt_from_layout.merge regexps
511 end
db045dbb » dhh 2004-11-23 Initial 512 end
513
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 514 public
db045dbb » dhh 2004-11-23 Initial 515 # Extracts the action_name from the request parameters and performs that action.
0ffb6c16 » dhh 2004-12-06 Syntax errors and other exc... 516 def process(request, response, method = :perform_action, *arguments) #:nodoc:
db045dbb » dhh 2004-11-23 Initial 517 initialize_template_class(response)
518 assign_shortcuts(request, response)
519 initialize_current_url
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 520 assign_names
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 521 forget_variables_added_to_assigns
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 522
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 523 log_processing
0ffb6c16 » dhh 2004-12-06 Syntax errors and other exc... 524 send(method, *arguments)
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 525
2caf4d5a » dhh 2006-09-17 Added proper getters and se... 526 assign_default_content_type_and_charset
7ec0204e » dhh 2007-02-18 Move etagging down to respo... 527
528 response.request = request
3f336ad5 » jeremy 2007-05-27 Don't prepare response when... 529 response.prepare! unless component_request?
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 530 response
ddf69109 » jeremy 2005-11-01 ensure close_session in AC:... 531 ensure
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 532 process_cleanup
db045dbb » dhh 2004-11-23 Initial 533 end
534
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 535 # Returns a URL that has been rewritten according to the options hash and the defined Routes.
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 536 # (For doing a complete redirect, use redirect_to).
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 537 #  
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 538 # <tt>url_for</tt> is used to:
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 539 #  
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 540 # All keys given to url_for are forwarded to the Route module, save for the following:
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 541 # * <tt>:anchor</tt> -- specifies the anchor name to be appended to the path. For example,
542 # <tt>url_for :controller => 'posts', :action => 'show', :id => 10, :anchor => 'comments'</tt>
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 543 # will produce "/posts/show/10#comments".
6ef5b747 » Marcel Molina 2006-05-14 Fix flip flopped logic in d... 544 # * <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... 545 # * <tt>:trailing_slash</tt> -- if true, adds a trailing slash, as in "/archive/2005/". Note that this
546 # is currently not recommended since it breaks caching.
3280a6e5 » NZKoz 2007-08-16 Improve url_for documentati... 547 # * <tt>:host</tt> -- overrides the default (current) host if provided.
548 # * <tt>:protocol</tt> -- overrides the default (current) protocol if provided.
549 # * <tt>:port</tt> -- optionally specify the port to connect to.
550 # * <tt>:user</tt> -- Inline HTTP authentication (only plucked out if :password is also present).
551 # * <tt>:password</tt> -- Inline HTTP authentication (only plucked out if :user is also present).
552 # * <tt>:skip_relative_url_root</tt> -- if true, the url is not constructed using the relative_url_root of the request so the path
553 # will include the web server relative installation directory.
e4efcfd4 » dhh 2005-02-23 Updated documentation 554 #
41ea6963 » dhh 2005-02-23 Added Base#render_to_string... 555 # The URL is generated from the remaining keys in the hash. A URL contains two key parts: the <base> and a query string.
556 # Routes composes a query string as the key/value pairs not included in the <base>.
e4efcfd4 » dhh 2005-02-23 Updated documentation 557 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 558 # The default Routes setup supports a typical Rails path of "controller/action/id" where action and id are optional, with
559 # action defaulting to 'index' when not given. Here are some typical url_for statements and their corresponding URLs:
3280a6e5 » NZKoz 2007-08-16 Improve url_for documentati... 560 #
561 # url_for :controller => 'posts', :action => 'recent' # => 'proto://host.com/posts/recent'
562 # url_for :controller => 'posts', :action => 'index' # => 'proto://host.com/posts'
563 # url_for :controller => 'posts', :action => 'index', :port=>'8033' # => 'proto://host.com:8033/posts'
564 # url_for :controller => 'posts', :action => 'show', :id => 10 # => 'proto://host.com/posts/show/10'
565 # url_for :controller => 'posts', :user => 'd', :password => '123' # => 'proto://d:123@host.com/posts'
e4efcfd4 » dhh 2005-02-23 Updated documentation 566 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 567 # When generating a new URL, missing values may be filled in from the current request's parameters. For example,
568 # <tt>url_for :action => 'some_action'</tt> will retain the current controller, as expected. This behavior extends to
569 # other parameters, including <tt>:controller</tt>, <tt>:id</tt>, and any other parameters that are placed into a Route's
570 # path.
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 571 #  
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 572 # The URL helpers such as <tt>url_for</tt> have a limited form of memory: when generating a new URL, they can look for
573 # missing values in the current request's parameters. Routes attempts to guess when a value should and should not be
574 # taken from the defaults. There are a few simple rules on how this is performed:
e4efcfd4 » dhh 2005-02-23 Updated documentation 575 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 576 # * If the controller name begins with a slash, no defaults are used: <tt>url_for :controller => '/home'</tt>
577 # * If the controller changes, the action will default to index unless provided
e4efcfd4 » dhh 2005-02-23 Updated documentation 578 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 579 # The final rule is applied while the URL is being generated and is best illustrated by an example. Let us consider the
580 # route given by <tt>map.connect 'people/:last/:first/:action', :action => 'bio', :controller => 'people'</tt>.
e4efcfd4 » dhh 2005-02-23 Updated documentation 581 #
82f1e19e » dhh 2005-10-26 Fixed docs (closes #2468) 582 # 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... 583 # from this page.
e4efcfd4 » dhh 2005-02-23 Updated documentation 584 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 585 # * <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 586 # 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... 587 # * <tt>url_for :first => 'davids-little-brother'</tt> This generates the URL 'people/hh/davids-little-brother' -- note
588 # that this URL leaves out the assumed action of 'bio'.
e4efcfd4 » dhh 2005-02-23 Updated documentation 589 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 590 # However, you might ask why the action from the current request, 'contacts', isn't carried over into the new URL. The
591 # answer has to do with the order in which the parameters appear in the generated path. In a nutshell, since the
592 # value that appears in the slot for <tt>:first</tt> is not equal to default value for <tt>:first</tt> we stop using
2af36bbb » dhh 2007-12-05 Fix typos (closes #10378) 593 # defaults. On its own, this rule can account for much of the typical Rails URL behavior.
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 594 #  
e3b49c05 » dhh 2007-09-28 Fixed spelling errors (clos... 595 # Although a convenience, defaults can occasionally get in your way. In some cases a default persists longer than desired.
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 596 # The default may be cleared by adding <tt>:name => nil</tt> to <tt>url_for</tt>'s options.
597 # This is often required when writing form helpers, since the defaults in play may vary greatly depending upon where the
598 # helper is used from. The following line will redirect to PostController's default action, regardless of the page it is
599 # displayed on:
db045dbb » dhh 2004-11-23 Initial 600 #
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 601 # url_for :controller => 'posts', :action => nil
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 602 #
c8e0e10e » dhh 2005-09-09 Added documentation for ove... 603 # If you explicitly want to create a URL that's almost the same as the current URL, you can do so using the
604 # :overwrite_params options. Say for your posts you have different views for showing and printing them.
605 # Then, in the show view, you get the URL for the print view like this
606 #
607 # url_for :overwrite_params => { :action => 'print' }
608 #
609 # 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) 610 # would have slashed-off the path components after the changed action.
f770165c » jeremy 2007-03-12 Deprecation: remove depreca... 611 def url_for(options = nil) #:doc:
612 case options || {}
4bd444c1 » dhh 2006-09-03 More deprecation fun 613 when String
614 options
615 when Hash
616 @url.rewrite(rewrite_options(options))
f770165c » jeremy 2007-03-12 Deprecation: remove depreca... 617 else
1ac7cd56 » jeremy 2007-05-17 Clean up the simply_helpful... 618 polymorphic_url(options)
db045dbb » dhh 2004-11-23 Initial 619 end
620 end
621
622 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "NeatController".
623 def controller_class_name
624 self.class.controller_class_name
625 end
626
627 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "neat".
628 def controller_name
629 self.class.controller_name
630 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 631
12ab93b7 » jeremy 2006-08-04 Make controller_path availa... 632 # Converts the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat".
633 def controller_path
634 self.class.controller_path
635 end
68d68505 » Tobias Lütke 2007-09-06 Remove deprecated named rou... 636
bea737eb » sstephenson 2005-11-22 Make ActionController's ren... 637 def session_enabled?
df7ca38d » jeremy 2007-02-26 session_enabled? works with... 638 request.session_options && request.session_options[:disabled] != false
bea737eb » sstephenson 2005-11-22 Make ActionController's ren... 639 end
68d68505 » Tobias Lütke 2007-09-06 Remove deprecated named rou... 640
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 641 self.view_paths = []
642
69b0e5c4 » technoweenie 2007-02-04 Allow Controllers to have m... 643 # View load paths for controller.
644 def view_paths
742694e0 » technoweenie 2007-10-25 Simplfy #view_paths impleme... 645 (@template || self.class).view_paths
646 end
647
648 def view_paths=(value)
649 (@template || self.class).view_paths = value
69b0e5c4 » technoweenie 2007-02-04 Allow Controllers to have m... 650 end
87e506da » technoweenie 2007-11-25 Add #prepend_view_path and ... 651
652 # Adds a view_path to the front of the view_paths array.
653 # This change affects the current request only.
654 #
655 # self.prepend_view_path("views/default")
656 # self.prepend_view_path(["views/default", "views/custom"])
657 #
658 def prepend_view_path(path)
659 (@template || self.class).prepend_view_path(path)
660 end
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 661
87e506da » technoweenie 2007-11-25 Add #prepend_view_path and ... 662 # Adds a view_path to the end of the view_paths array.
663 # This change affects the current request only.
664 #
665 # self.append_view_path("views/default")
666 # self.append_view_path(["views/default", "views/custom"])
667 #
668 def append_view_path(path)
669 (@template || self.class).append_view_path(path)
670 end
671
db045dbb » dhh 2004-11-23 Initial 672 protected
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 673 # Renders the content that will be returned to the browser as the response body.
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 674 #
675 # === Rendering an action
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 676 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 677 # Action rendering is the most common form and the type used automatically by Action Controller when nothing else is
678 # specified. By default, actions are rendered within the current layout (if one exists).
679 #
680 # # Renders the template for the action "goal" within the current controller
681 # render :action => "goal"
682 #
683 # # Renders the template for the action "short_goal" within the current controller,
684 # # but without the current active layout
685 # render :action => "short_goal", :layout => false
686 #
687 # # Renders the template for the action "long_goal" within the current controller,
688 # # but with a custom layout
1aab0e2c » dhh 2005-07-21 Doc fixes #1775, #1776 [jon... 689 # render :action => "long_goal", :layout => "spectacular"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 690 #
691 # === Rendering partials
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 692 #
693 # 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... 694 # without reloading. Rendering of partials from the controller makes it possible to use the same partial template in
695 # both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the
696 # controller action responding to Ajax calls). By default, the current layout is not used.
697 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 698 # # Renders the same partial with a local variable.
699 # render :partial => "person", :locals => { :name => "david" }
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 700 #
8d8219cc » dhh 2007-06-23 Docfix (closes #8518) 701 # # Renders the partial, making @new_person available through
702 # # the local variable 'person'
703 # render :partial => "person", :object => @new_person
704 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 705 # # Renders a collection of the same partial by making each element
706 # # of @winners available through the local variable "person" as it
707 # # builds the complete response.
708 # render :partial => "person", :collection => @winners
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 709 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 710 # # Renders the same collection of partials, but also renders the
711 # # person_divider partial between each person partial.
712 # render :partial => "person", :collection => @winners, :spacer_template => "person_divider"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 713 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 714 # # Renders a collection of partials located in a view subfolder
715 # # outside of our current controller. In this example we will be
716 # # rendering app/views/shared/_note.r(html|xml) Inside the partial
717 # # each element of @new_notes is available as the local var "note".
718 # render :partial => "shared/note", :collection => @new_notes
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 719 #
e5cbb849 » jeremy 2006-07-07 Update render :partial docu... 720 # # Renders the partial with a status code of 500 (internal error).
721 # render :partial => "broken", :status => 500
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 722 #
cf965cda » jeremy 2006-07-07 Clarify partial filename co... 723 # Note that the partial filename must also be a valid Ruby variable name,
724 # so e.g. 2005 and register-user are invalid.
725 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 726 #
2e55095f » dhh 2007-02-17 Added that rendering will a... 727 # == Automatic etagging
728 #
729 # Rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the
730 # response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified
109d4ac9 » dhh 2007-02-18 Allow people to set their o... 731 # and the response body will be set to an empty string. No etag header will be inserted if it's already set.
2e55095f » dhh 2007-02-17 Added that rendering will a... 732 #
f510b09c » dhh 2005-10-03 Made the documentation abou... 733 # === Rendering a template
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 734 #
735 # Template rendering works just like action rendering except that it takes a path relative to the template root.
f510b09c » dhh 2005-10-03 Made the documentation abou... 736 # The current layout is automatically applied.
fede0f57 » dhh 2005-07-11 Fixed documentation for :ac... 737 #
e1056530 » dhh 2007-02-20 Added .erb and .builder as ... 738 # # Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
f510b09c » dhh 2005-10-03 Made the documentation abou... 739 # render :template => "weblog/show"
740 #
741 # === Rendering a file
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 742 #
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 743 # File rendering works just like action rendering except that it takes a filesystem path. By default, the path
744 # is assumed to be absolute, and the current layout is not applied.
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 745 #
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 746 # # Renders the template located at the absolute filesystem path
e1056530 » dhh 2007-02-20 Added .erb and .builder as ... 747 # render :file => "/path/to/some/template.erb"
748 # render :file => "c:/path/to/some/template.erb"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 749 #
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 750 # # Renders a template within the current layout, and with a 404 status code
e1056530 » dhh 2007-02-20 Added .erb and .builder as ... 751 # render :file => "/path/to/some/template.erb", :layout => true, :status => 404
752 # render :file => "c:/path/to/some/template.erb", :layout => true, :status => 404
5cd2f326 » jeremy 2005-11-13 Update documentation for re... 753 #
754 # # Renders a template relative to the template root and chooses the proper file extension
755 # render :file => "some/template", :use_full_path => true
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 756 #
757 # === Rendering text
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 758 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 759 # Rendering of text is usually used for tests or for rendering prepared content, such as a cache. By default, text
760 # rendering is not done within the active layout.
761 #
762 # # Renders the clear text "hello world" with status code 200
763 # render :text => "hello world!"
764 #
765 # # Renders the clear text "Explosion!" with status code 500
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 766 # render :text => "Explosion!", :status => 500
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 767 #
768 # # Renders the clear text "Hi there!" within the current active layout (if one exists)
a0c925c0 » Marcel Molina 2007-11-26 Minor inconsistency in desc... 769 # render :text => "Hi there!", :layout => true
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 770 #
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 771 # # Renders the clear text "Hi there!" within the layout
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 772 # # placed in "app/views/layouts/special.r(html|xml)"
a0c925c0 » Marcel Molina 2007-11-26 Minor inconsistency in desc... 773 # render :text => "Hi there!", :layout => "special"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 774 #
40f50fd7 » dhh 2006-07-04 Doc fix (closes #5429) 775 # The :text option can also accept a Proc object, which can be used to manually control the page generation. This should
776 # generally be avoided, as it violates the separation between code and content, and because almost everything that can be
777 # done with this method can also be done more cleanly using one of the other rendering methods, most notably templates.
778 #
779 # # Renders "Hello from code!"
780 # render :text => proc { |response, output| output.write("Hello from code!") }
781 #
42596543 » jeremy 2006-12-06 respond_to recognizes JSON.... 782 # === Rendering JSON
783 #
fe234a15 » NZKoz 2007-10-15 Fix Json related documentat... 784 # Rendering JSON sets the content type to application/json and optionally wraps the JSON in a callback. It is expected
785 # that the response will be parsed (or eval'd) for use as a data structure.
42596543 » jeremy 2006-12-06 respond_to recognizes JSON.... 786 #
fe234a15 » NZKoz 2007-10-15 Fix Json related documentat... 787 # # Renders '{"name": "David"}'
42596543 » jeremy 2006-12-06 respond_to recognizes JSON.... 788 # render :json => {:name => "David"}.to_json
789 #
fe234a15 » NZKoz 2007-10-15 Fix Json related documentat... 790 # It's not necessary to call <tt>to_json</tt> on the object you want to render, since <tt>render</tt> will
791 # automatically do that for you:
792 #
793 # # Also renders '{"name": "David"}'
794 # render :json => {:name => "David"}
795 #
42596543 » jeremy 2006-12-06 respond_to recognizes JSON.... 796 # Sometimes the result isn't handled directly by a script (such as when the request comes from a SCRIPT tag),
fe234a15 » NZKoz 2007-10-15 Fix Json related documentat... 797 # so the <tt>:callback</tt> option is provided for these cases.
42596543 » jeremy 2006-12-06 respond_to recognizes JSON.... 798 #
fe234a15 » NZKoz 2007-10-15 Fix Json related documentat... 799 # # Renders 'show({"name": "David"})'
42596543 » jeremy 2006-12-06 respond_to recognizes JSON.... 800 # render :json => {:name => "David"}.to_json, :callback => 'show'
801 #
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 802 # === Rendering an inline template
803 #
804 # Rendering of an inline template works as a cross between text and action rendering where the source for the template
805 # is supplied inline, like text, but its interpreted with ERb or Builder, like action. By default, ERb is used for rendering
806 # and the current layout is not used.
807 #
808 # # Renders "hello, hello, hello, again"
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 809 # render :inline => "<%= 'hello, ' * 3 + 'again' %>"
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 810 #
811 # # Renders "<p>Good seeing you!</p>" using Builder
e1056530 » dhh 2007-02-20 Added .erb and .builder as ... 812 # render :inline => "xml.p { 'Good seeing you!' }", :type => :builder
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 813 #
814 # # Renders "hello david"
815 # render :inline => "<%= 'hello ' + name %>", :locals => { :name => "david" }
816 #
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 817 # === Rendering inline JavaScriptGenerator page updates
818 #
819 # In addition to rendering JavaScriptGenerator page updates with Ajax in RJS templates (see ActionView::Base for details),
820 # you can also pass the <tt>:update</tt> parameter to +render+, along with a block, to render page updates inline.
821 #
822 # render :update do |page|
823 # page.replace_html 'user_list', :partial => 'user', :collection => @users
824 # page.visual_effect :highlight, 'user_list'
825 # end
826 #
5208d8d8 » dhh 2007-04-24 Added :location option to r... 827 # === Rendering with status and location headers
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 828 #
5208d8d8 » dhh 2007-04-24 Added :location option to r... 829 # All renders take the :status and :location options and turn them into headers. They can even be used together:
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 830 #
5208d8d8 » dhh 2007-04-24 Added :location option to r... 831 # render :xml => post.to_xml, :status => :created, :location => post_url(post)
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 832 def render(options = nil, &block) #:doc:
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 833 raise DoubleRenderError, "Can only render or redirect once per action" if performed?
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 834
5048aa98 » dhh 2006-09-03 Deprecated old render param... 835 if options.nil?
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 836 return render_for_file(default_template_name, nil, true)
5048aa98 » dhh 2006-09-03 Deprecated old render param... 837 else
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 838 if options == :update
839 options = { :update => true }
840 elsif !options.is_a?(Hash)
841 raise RenderError, "You called render with invalid options : #{options}"
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 842 end
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 843 end
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 844
06c2b43f » dhh 2006-03-12 Rendering xml shouldnt happ... 845 if content_type = options[:content_type]
2caf4d5a » dhh 2006-09-17 Added proper getters and se... 846 response.content_type = content_type.to_s
06c2b43f » dhh 2006-03-12 Rendering xml shouldnt happ... 847 end
848
5208d8d8 » dhh 2007-04-24 Added :location option to r... 849 if location = options[:location]
ebee0a74 » dhh 2007-05-17 Added url_for usage on rend... 850 response.headers["Location"] = url_for(location)
5208d8d8 » dhh 2007-04-24 Added :location option to r... 851 end
852
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 853 if text = options[:text]
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 854 render_for_text(text, options[:status])
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 855
856 else
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 857 if file = options[:file]
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 858 render_for_file(file, options[:status], options[:use_full_path], options[:locals] || {})
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 859
860 elsif template = options[:template]
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 861 render_for_file(template, options[:status], true)
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 862
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 863 elsif inline = options[:inline]
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 864 add_variables_to_assigns
9aca06fb » jeremy 2007-12-10 More Action View refactorin... 865 render_for_text(@template.render_template(options[:type], inline, nil, options[:locals] || {}), options[:status])
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 866
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 867 elsif action_name = options[:action]
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 868 template = default_template_name(action_name.to_s)
869 if options[:layout] && !template_exempt_from_layout?(template)
870 render_with_a_layout(:file => template, :status => options[:status], :use_full_path => true, :layout => true)
871 else
872 render_with_no_layout(:file => template, :status => options[:status], :use_full_path => true)
873 end
1c16649b » dhh 2006-03-10 Added better support for us... 874
875 elsif xml = options[:xml]
bafd698a » jeremy 2007-12-09 render :xml and :json prese... 876 response.content_type ||= Mime::XML
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 877 render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status])
953214bd » jeremy 2006-12-06 Remove unrelated render :ya... 878
42596543 » jeremy 2006-12-06 respond_to recognizes JSON.... 879 elsif json = options[:json]
1373991d » dhh 2007-09-20 Added that render :json wil... 880 json = json.to_json unless json.is_a?(String)
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 881 json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
bafd698a » jeremy 2007-12-09 render :xml and :json prese... 882 response.content_type ||= Mime::JSON
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 883 render_for_text(json, options[:status])
1c16649b » dhh 2006-03-10 Added better support for us... 884
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 885 elsif partial = options[:partial]
886 partial = default_template_name if partial == true
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 887 add_variables_to_assigns
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 888
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 889 if collection = options[:collection]
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 890 render_for_text(
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 891 @template.send!(:render_partial_collection, partial, collection,
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 892 options[:spacer_template], options[:locals]), options[:status]
893 )
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 894 else
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 895 render_for_text(
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 896 @template.send!(:render_partial, partial,
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 897 ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]), options[:status]
898 )
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 899 end
900
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 901 elsif options[:update]
d2adec43 » seckar 2006-01-29 Ensure assigns are copied t... 902 add_variables_to_assigns
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 903 @template.send! :evaluate_assigns
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 904
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 905 generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block)
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 906 response.content_type = Mime::JS
907 render_for_text(generator.to_s)
06dd1f2c » sstephenson 2006-01-20 Add render :update for inli... 908
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 909 elsif options[:nothing]
910 # Safari doesn't pass the headers of the return if the response is zero length
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 911 render_for_text(" ", options[:status])
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 912
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 913 else
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 914 render_for_file(default_template_name, options[:status], true)
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 915 end
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 916 end
917 end
db045dbb » dhh 2004-11-23 Initial 918
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 919 # Renders according to the same rules as <tt>render</tt>, but returns the result in a string instead
920 # of sending it as the response body to the browser.
f9937dd0 » sstephenson 2006-01-20 Pass along blocks from rend... 921 def render_to_string(options = nil, &block) #:doc:
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 922 render(options, &block)
6fc8e143 » jeremy 2006-11-20 Ensure render_to_string cle... 923 ensure
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 924 erase_render_results
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 925 forget_variables_added_to_assigns
926 reset_variables_added_to_assigns
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 927 end
4f40b2d8 » dhh 2005-07-24 Improved performance of tes... 928
b2ede64a » jamis 2006-09-28 Add ActionController::Base#... 929 # Return a response that has no content (merely headers). The options
930 # argument is interpreted to be a hash of header names and values.
931 # This allows you to easily return a response that consists only of
932 # significant headers:
933 #
c0eccc9a » jamis 2006-09-28 modify head so that you can... 934 # head :created, :location => person_path(@person)
b2ede64a » jamis 2006-09-28 Add ActionController::Base#... 935 #
936 # It can also be used to return exceptional conditions:
937 #
c0eccc9a » jamis 2006-09-28 modify head so that you can... 938 # return head(:method_not_allowed) unless request.post?
939 # return head(:bad_request) unless valid_request?
b2ede64a » jamis 2006-09-28 Add ActionController::Base#... 940 # render
c0eccc9a » jamis 2006-09-28 modify head so that you can... 941 def head(*args)
942 if args.length > 2
943 raise ArgumentError, "too many arguments to head"
944 elsif args.empty?
945 raise ArgumentError, "too few arguments to head"
946 end
d1c6349e » technoweenie 2007-12-08 Clean up some cruft around ... 947 options = args.extract_options!
948 status = interpret_status(args.shift || options.delete(:status) || :ok)
b2ede64a » jamis 2006-09-28 Add ActionController::Base#... 949
950 options.each do |key, value|
951 headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s
952 end
953
954 render :nothing => true, :status => status
955 end
956
957
62ed6950 » dhh 2005-06-28 Added support for upload pr... 958 # Clears the rendered results, allowing for another render to be performed.
dfd953ea » dhh 2006-03-27 Fixed docs 959 def erase_render_results #:nodoc:
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 960 response.body = nil
dab360e1 » dhh 2005-05-21 Added DoubleRenderError exc... 961 @performed_render = false
962 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 963
964 # Clears the redirected results from the headers, resets the status to 200 and returns
62ed6950 » dhh 2005-06-28 Added support for upload pr... 965 # the URL that was used to redirect or nil if there was no redirected URL
966 # Note that +redirect_to+ will change the body of the response to indicate a redirection.
967 # The response body is not reset here, see +erase_render_results+
dfd953ea » dhh 2006-03-27 Fixed docs 968 def erase_redirect_results #:nodoc:
62ed6950 » dhh 2005-06-28 Added support for upload pr... 969 @performed_redirect = false
970 response.redirected_to = nil
971 response.redirected_to_method_params = nil
972 response.headers['Status'] = DEFAULT_RENDER_STATUS_CODE
4887e53b » jeremy 2006-11-25 Use Location rather than lo... 973 response.headers.delete('Location')
62ed6950 » dhh 2005-06-28 Added support for upload pr... 974 end
975
e9cfe955 » dhh 2005-07-25 Add a catch-all eraser 976 # Erase both render and redirect results
dfd953ea » dhh 2006-03-27 Fixed docs 977 def erase_results #:nodoc:
e9cfe955 » dhh 2005-07-25 Add a catch-all eraser 978 erase_render_results
979 erase_redirect_results
980 end
93ec99c2 » dhh 2005-07-06 Partly tuned docs for relea... 981
dfd953ea » dhh 2006-03-27 Fixed docs 982 def rewrite_options(options) #:nodoc:
db045dbb » dhh 2004-11-23 Initial 983 if defaults = default_url_options(options)
984 defaults.merge(options)
985 else
986 options
987 end
988 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 989
db045dbb » dhh 2004-11-23 Initial 990 # Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in
991 # the form of a hash, just like the one you would use for url_for directly. Example:
992 #
993 # def default_url_options(options)
5c4f1859 » dhh 2005-02-23 Fixed that send_file/data c... 994 # { :project => @project.active? ? @project.url_name : "unknown" }
db045dbb » dhh 2004-11-23 Initial 995 # end
996 #
997 # As you can infer from the example, this is mostly useful for situations where you want to centralize dynamic decisions about the
998 # urls as they stem from the business domain. Please note that any individual url_for call can always override the defaults set
999 # by this method.
1000 def default_url_options(options) #:doc:
1001 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1002
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1003 # Redirects the browser to the target specified in +options+. This parameter can take one of three forms:
1004 #
94502623 » Marcel Molina 2007-11-06 Standardize on using hyphen... 1005 # * <tt>Hash</tt> - The URL will be generated by calling url_for with the +options+.
1006 # * <tt>Record</tt> - The URL will be generated by calling url_for with the +options+, which will reference a named URL for that record.
1007 # * <tt>String starting with protocol:// (like http://)</tt> - Is passed straight through as the target for redirection.
1008 # * <tt>String not containing a protocol</tt> - The current protocol and host is prepended to the string.
1009 # * <tt>:back</tt> - Back to the page that issued the request. Useful for forms that are triggered from multiple places.
4f754985 » dhh 2005-11-02 Added redirect_to :back as ... 1010 # Short-hand for redirect_to(request.env["HTTP_REFERER"])
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1011 #
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1012 # Examples:
1013 # redirect_to :action => "show", :id => 5
6e7b5939 » dhh 2007-05-14 Added record identification... 1014 # redirect_to post
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1015 # redirect_to "http://www.rubyonrails.org"
1016 # redirect_to "/images/screenshot.jpg"
5af44634 » Marcel Molina 2007-12-05 Add example of redirect_to ... 1017 # redirect_to articles_url
4f754985 » dhh 2005-11-02 Added redirect_to :back as ... 1018 # redirect_to :back
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1019 #
4aabe463 » NZKoz 2007-10-09 Add :status to redirect_to ... 1020 # The redirection happens as a "302 Moved" header unless otherwise specified.
1021 #
1022 # Examples:
1023 # redirect_to post_url(@post), :status=>:found
1024 # redirect_to :action=>'atom', :status=>:moved_permanently
1025 # redirect_to post_url(@post), :status=>301
1026 # redirect_to :action=>'atom', :status=>302
21142201 » Marcel Molina 2006-04-24 Add documentation for redir... 1027 #
1028 # When using <tt>redirect_to :back</tt>, if there is no referrer,
1029 # RedirectBackError will be raised. You may specify some fallback
e3b49c05 » dhh 2007-09-28 Fixed spelling errors (clos... 1030 # behavior for this case by rescuing RedirectBackError.
4aabe463 » NZKoz 2007-10-09 Add :status to redirect_to ... 1031 def redirect_to(options = {}, response_status = {}) #:doc:
1032
1033 if options.is_a?(Hash) && options[:status]
1034 status = options.delete(:status)
1035 elsif response_status[:status]
1036 status = response_status[:status]
1037 else
1038 status = 302
1039 end
1040
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1041 case options
1042 when %r{^\w+://.*}
82668678 » jamis 2005-07-09 Improved error message for ... 1043 raise DoubleRenderError if performed?
4aabe463 » NZKoz 2007-10-09 Add :status to redirect_to ... 1044 logger.info("Redirected to #{options}") if logger && logger.info?
1045 response.redirect(options, interpret_status(status))
060ecf1a » dhh 2005-05-22 Set redirected_to proper 1046 response.redirected_to = options
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1047 @performed_redirect = true
1048
1049 when String
4aabe463 » NZKoz 2007-10-09 Add :status to redirect_to ... 1050 redirect_to(request.protocol + request.host_with_port + options, :status=>status)
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1051
4f754985 » dhh 2005-11-02 Added redirect_to :back as ... 1052 when :back
4aabe463 » NZKoz 2007-10-09 Add :status to redirect_to ... 1053 request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"], :status=>status) : raise(RedirectBackError)
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1054
d8d6ef8a » jeremy 2007-05-17 Oops. 1055 when Hash
4aabe463 » NZKoz 2007-10-09 Add :status to redirect_to ... 1056 redirect_to(url_for(options), :status=>status)
d8d6ef8a » jeremy 2007-05-17 Oops. 1057 response.redirected_to = options
1058
0367317d » dhh 2005-05-22 Deprecated redirect_to_path... 1059 else
4aabe463 » NZKoz 2007-10-09 Add :status to redirect_to ... 1060 redirect_to(url_for(options), :status=>status)
db045dbb » dhh 2004-11-23 Initial 1061 end
1062 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1063
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 1064 # Sets a HTTP 1.1 Cache-Control header. Defaults to issuing a "private" instruction, so that
1065 # intermediate caches shouldn't cache the response.
1066 #
1067 # Examples:
1068 # expires_in 20.minutes
1069 # expires_in 3.hours, :private => false
1070 # expires in 3.hours, 'max-stale' => 5.hours, :private => nil, :public => true
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1071 #
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 1072 # This method will overwrite an existing Cache-Control header.
1073 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities.
d1725929 » dhh 2005-07-23 Added test for template to ... 1074 def expires_in(seconds, options = {}) #:doc:
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 1075 cache_options = { 'max-age' => seconds, 'private' => true }.symbolize_keys.merge!(options.symbolize_keys)
1076 cache_options.delete_if { |k,v| v.nil? or v == false }
1077 cache_control = cache_options.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1078 response.headers["Cache-Control"] = cache_control.join(', ')
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 1079 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1080
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 1081 # Sets a HTTP 1.1 Cache-Control header of "no-cache" so no caching should occur by the browser or
1082 # intermediate caches (like caching proxy servers).
d1725929 » dhh 2005-07-23 Added test for template to ... 1083 def expires_now #:doc:
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1084 response.headers["Cache-Control"] = "no-cache"
d2d38f67 » dhh 2005-07-17 Added Base#expires_in(secon... 1085 end
db045dbb » dhh 2004-11-23 Initial 1086
098fa943 » dhh 2005-02-07 Fixed documentation snafus ... 1087 # Resets the session by clearing out all the objects stored within and initializing a new session object.
db045dbb » dhh 2004-11-23 Initial 1088 def reset_session #:doc:
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1089 request.reset_session
1090 @_session = request.session
1091 response.session = @_session
db045dbb » dhh 2004-11-23 Initial 1092 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1093
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 1094
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 1095 private
f81dae3f » NZKoz 2007-09-02 Remove deprecated functiona... 1096 def render_for_file(template_path, status = nil, use_full_path = false, locals = {}) #:nodoc:
1097 add_variables_to_assigns
1098 assert_existence_of_template_file(template_path) if use_full_path
1099 logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger
1100 render_for_text(@template.render_file(template_path, use_full_path, locals), status)
1101 end
1102
1103 def render_for_text(text = nil, status = nil, append_response = false) #:nodoc:
1104 @performed_render = true
1105
1106 response.headers['Status'] = interpret_status(status || DEFAULT_RENDER_STATUS_CODE)
1107
1108 if append_response
1109 response.body ||= ''
1110 response.body << text.to_s
1111 else
1112 response.body = text.is_a?(Proc) ? text : text.to_s
1113 end
1114 end
1115
bf92aacf » seckar 2005-09-08 Avoid extending view instan... 1116 def initialize_template_class(response)
ff9ca2ca » dhh 2007-09-09 Random hits from the style ... 1117 unless @@template_class
1118 raise "You must assign a template class through ActionController.template_class= before processing a request"
1119 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1120
301b9237 » jeremy 2007-05-23 Extend the view instance di... 1121 response.template = ActionView::Base.new(view_paths, {}, self)
1122 response.template.extend self.class.master_helper_module
02594910 » rubyist 2005-10-30 Fix problem where redirecti... 1123 response.redirected_to = nil
db045dbb » dhh 2004-11-23 Initial 1124 @performed_render = @performed_redirect = false
1125 end
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1126
db045dbb » dhh 2004-11-23 Initial 1127 def assign_shortcuts(request, response)
4f3bf6cb » jeremy 2006-09-29 Deprecate @cookies 1128 @_request, @_params, @_cookies = request, request.parameters, request.cookies
db045dbb » dhh 2004-11-23 Initial 1129
d15d15b2 » jeremy 2006-09-29 Deprecate @response 1130 @_response = response
1131 @_response.session = request.session
db045dbb » dhh 2004-11-23 Initial 1132
d15d15b2 » jeremy 2006-09-29 Deprecate @response 1133 @_session = @_response.session
1134 @template = @_response.template
1135 @assigns = @_response.template.assigns
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1136
d15d15b2 » jeremy 2006-09-29 Deprecate @response 1137 @_headers = @_response.headers
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1138 end
1139
db045dbb » dhh 2004-11-23 Initial 1140 def initialize_current_url
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1141 @url = UrlRewriter.new(request, params.clone)
db045dbb » dhh 2004-11-23 Initial 1142 end
1143
1144 def log_processing
551f6e9b » jeremy 2007-10-06 Don't generate strings to l... 1145 if logger && logger.info?
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 1146 logger.info "\n\nProcessing #{controller_class_name}\##{action_name} (for #{request_origin}) [#{request.method.to_s.upcase}]"
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1147 logger.info " Session ID: #{@_session.session_id}" if @_session and @_session.respond_to?(:session_id)
1148 logger.info " Parameters: #{respond_to?(:filter_parameters) ? filter_parameters(params).inspect : params.inspect}"
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 1149 end
db045dbb » dhh 2004-11-23 Initial 1150 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1151
12d8d48b » NZKoz 2007-10-24 Refactor the default render... 1152 def default_render #:nodoc:
1153 render
1154 end
1155
db045dbb » dhh 2004-11-23 Initial 1156 def perform_action
84bacf99 » seckar 2006-08-12 Invoke method_missing direc... 1157 if self.class.action_methods.include?(action_name)
db045dbb » dhh 2004-11-23 Initial 1158 send(action_name)
12d8d48b » NZKoz 2007-10-24 Refactor the default render... 1159 default_render unless performed?
84bacf99 » seckar 2006-08-12 Invoke method_missing direc... 1160 elsif respond_to? :method_missing
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 1161 method_missing action_name
12d8d48b » NZKoz 2007-10-24 Refactor the default render... 1162 default_render unless performed?
db045dbb » dhh 2004-11-23 Initial 1163 elsif template_exists? && template_public?
12d8d48b » NZKoz 2007-10-24 Refactor the default render... 1164 default_render
db045dbb » dhh 2004-11-23 Initial 1165 else
1166 raise UnknownAction, "No action responded to #{action_name}", caller
1167 end
1168 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1169
2ee84cc6 » dhh 2005-01-20 Fixed that all redirect and... 1170 def performed?
1171 @performed_render || @performed_redirect
1172 end
db045dbb » dhh 2004-11-23 Initial 1173
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1174 def assign_names
1175 @action_name = (params['action'] || 'index')
1176 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1177
2caf4d5a » dhh 2006-09-17 Added proper getters and se... 1178 def assign_default_content_type_and_charset
1179 response.content_type ||= Mime::HTML
396b3f2b » dhh 2006-11-25 Dont set default charset if... 1180 response.charset ||= self.class.default_charset unless sending_file?
1181 end
1182
1183 def sending_file?
1184 response.headers["Content-Transfer-Encoding"] == "binary"
2caf4d5a » dhh 2006-09-17 Added proper getters and se... 1185 end
1186
db045dbb » dhh 2004-11-23 Initial 1187 def action_methods
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 1188 self.class.action_methods
db045dbb » dhh 2004-11-23 Initial 1189 end
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 1190
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 1191 def self.action_methods
0ee1cb2c » jeremy 2007-10-01 Ruby 1.9 compat, consistent... 1192 @action_methods ||= Set.new(public_instance_methods.map(&:to_s)) - hidden_actions
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 1193 end
da0c4c5c » dhh 2005-05-22 Deprecated all render_* met... 1194
db045dbb » dhh 2004-11-23 Initial 1195 def add_variables_to_assigns
b366dbd9 » dhh 2005-07-23 Improved performance with 5... 1196 unless @variables_added
1197 add_instance_variables_to_assigns
1198 add_class_variables_to_assigns if view_controller_internals
1199 @variables_added = true
1200 end
db045dbb » dhh 2004-11-23 Initial 1201 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1202
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 1203 def forget_variables_added_to_assigns
1204 @variables_added = nil
1205 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1206
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 1207 def reset_variables_added_to_assigns
1208 @template.instance_variable_set("@assigns_added", nil)
1209 end
db045dbb » dhh 2004-11-23 Initial 1210
1211 def add_instance_variables_to_assigns
c3cdd3b6 » jeremy 2006-08-07 Deprecation: check whether ... 1212 @@protected_variables_cache ||= Set.new(protected_instance_variables)
db045dbb » dhh 2004-11-23 Initial 1213 instance_variables.each do |var|
fa158ff0 » jeremy 2005-07-04 r2829@asus: jeremy | 2005... 1214 next if @@protected_variables_cache.include?(var)
db045dbb » dhh 2004-11-23 Initial 1215 @assigns[var[1..-1]] = instance_variable_get(var)
1216 end
1217 end
1218
1219 def add_class_variables_to_assigns
69b0e5c4 » technoweenie 2007-02-04 Allow Controllers to have m... 1220 %w(view_paths logger template_class ignore_missing_templates).each do |cvar|
db045dbb » dhh 2004-11-23 Initial 1221 @assigns[cvar] = self.send(cvar)
1222 end
1223 end
1224
1225 def protected_instance_variables
1226 if view_controller_internals
c40b1a4a » jeremy 2006-08-07 Deprecate direct usage of @... 1227 %w(@assigns @performed_redirect @performed_render)
db045dbb » dhh 2004-11-23 Initial 1228 else
c40b1a4a » jeremy 2006-08-07 Deprecate direct usage of @... 1229 %w(@assigns @performed_redirect @performed_render
d15d15b2 » jeremy 2006-09-29 Deprecate @response 1230 @_request @request @_response @response @_params @params
1231 @_session @session @_cookies @cookies
1232 @template @request_origin @parent_controller)
db045dbb » dhh 2004-11-23 Initial 1233 end
1234 end
1235
1236 def request_origin
76540822 » jeremy 2006-02-09 Major components cleanup an... 1237 # this *needs* to be cached!
1238 # otherwise you'd get different results if calling it more than once
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1239 @request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}"
db045dbb » dhh 2004-11-23 Initial 1240 end
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1241
977b4be2 » dhh 2005-07-05 Changed logging of SQL stat... 1242 def complete_request_uri
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1243 "#{request.protocol}#{request.host}#{request.request_uri}"
977b4be2 » dhh 2005-07-05 Changed logging of SQL stat... 1244 end
1245
db045dbb » dhh 2004-11-23 Initial 1246 def close_session
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1247 @_session.close if @_session && @_session.respond_to?(:close)
db045dbb » dhh 2004-11-23 Initial 1248 end
2399a223 » jeremy 2006-08-06 Deprecation! @session and @... 1249
db045dbb » dhh 2004-11-23 Initial 1250 def template_exists?(template_name = default_template_name)
1251 @template.file_exists?(template_name)
1252 end
1253
1254 def template_public?(template_name = default_template_name)
1255 @template.file_public?(template_name)
1256 end
1257
62fe5bbf » Marcel Molina 2005-11-18 Make rjs templates always i... 1258 def template_exempt_from_layout?(template_name = default_template_name)
ebf9b373 » technoweenie 2007-05-14 Add some performance enhanc... 1259 extension = @template && @template.pick_template_extension(template_name)
55efae27 » jeremy 2007-01-14 Allow exempt_from_layout :r... 1260 name_with_extension = !template_name.include?('.') && extension ? "#{template_name}.#{extension}" : template_name
6a35ed16 » jeremy 2007-10-22 Remove duplicate rjs layout... 1261 @@exempt_from_layout.any? { |ext| name_with_extension =~ ext }
62fe5bbf » Marcel Molina 2005-11-18 Make rjs templates always i... 1262 end
1263
b6e7cc63 » dhh 2006-03-27 Spell existence properly (c... 1264 def assert_existence_of_template_file(template_name)
db045dbb » dhh 2004-11-23 Initial 1265 unless template_exists?(template_name) || ignore_missing_templates
d4d4a08f » NZKoz 2007-07-11 Make sure missing template ... 1266 full_template_path = template_name.include?('.') ? template_name : "#{template_name}.#{@template.template_format}.erb"
1267 display_paths = view_paths.join(':')
db045dbb » dhh 2004-11-23 Initial 1268 template_type = (template_name =~ /layouts/i) ? 'layout' : 'template'
d4d4a08f » NZKoz 2007-07-11 Make sure missing template ... 1269 raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}")
db045dbb » dhh 2004-11-23 Initial 1270 end
1271 end
1272
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1273 def default_template_name(action_name = self.action_name)
1274 if action_name
1275 action_name = action_name.to_s
1276 if action_name.include?('/') && template_path_includes_controller?(action_name)
1277 action_name = strip_out_controller(action_name)
1278 end
233208b8 » Marcel Molina 2005-12-19 Don't try to strip out the ... 1279 end
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1280 "#{self.class.controller_path}/#{action_name}"
db045dbb » dhh 2004-11-23 Initial 1281 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1282
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1283 def strip_out_controller(path)
1284 path.split('/', 2).last
39766a9b » Marcel Molina 2005-12-16 Don't include a layout when... 1285 end
2dd58202 » jeremy 2006-09-03 silence deprecation warning... 1286
39766a9b » Marcel Molina 2005-12-16 Don't include a layout when... 1287 def template_path_includes_controller?(path)
d19e8f41 » dhh 2006-03-19 Performance speedup for Act... 1288 self.class.controller_path.split('/')[-1] == path.split('/')[0]
39766a9b » Marcel Molina 2005-12-16 Don't include a layout when... 1289 end
050c3964 » dhh 2006-02-11 Stopped the massive bleedin... 1290
1291 def process_cleanup
1292 close_session
1293 end
db045dbb » dhh 2004-11-23 Initial 1294 end
e6941149 » jeremy 2007-09-13 Deprecation: removed Reload... 1295 end