rails / rails

Ruby on Rails

This URL has Read+Write access

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