public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
1c16649b » dhh 2006-03-10 Added better support for us... 1 require 'action_controller/mime_type'
db045dbb » dhh 2004-11-23 Initial 2 require 'action_controller/request'
3 require 'action_controller/response'
b1999be5 » dhh 2005-02-14 A hopefully more successful... 4 require 'action_controller/routing'
865b1757 » dhh 2006-07-31 Added map.resources from th... 5 require 'action_controller/resources'
db045dbb » dhh 2004-11-23 Initial 6 require 'action_controller/url_rewriter'
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