Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4290

Show
Ignore:
Timestamp:
04/27/06 18:22:20 (2 years ago)
Author:
marcel
Message:

ActionController::Base Summary documentation rewrite. Closes #4900. [kevin.clark@gmail.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r4289 r4290  
    11*SVN* 
     2 
     3* ActionController::Base Summary documentation rewrite. Closes #4900. [kevin.clark@gmail.com] 
    24 
    35* Fix text_helper.rb documentation rendering. Closes #4725. [Frederick Ros] 
  • trunk/actionpack/lib/action_controller/base.rb

    r4268 r4290  
    5050  end 
    5151 
    52   # Action Controllers are made up of one or more actions that performs its purpose and then either renders a template or 
    53   # redirects to another action. An action is defined as a public method on the controller, which will automatically be  
    54   # made accessible to the web-server through a mod_rewrite mapping. A sample controller could look like this: 
     52  # Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed  
     53  # on request and then either render a template or redirect to another action. An action is defined as a public method 
     54  # on the controller, which will automatically be made accessible to the web-server through Rails Routes.  
     55  # 
     56  # A sample controller could look like this: 
    5557  # 
    5658  #   class GuestBookController < ActionController::Base 
    5759  #     def index 
    58   #       @entries = Entry.find_all 
     60  #       @entries = Entry.find(:all) 
    5961  #     end 
    6062  #      
     
    6567  #   end 
    6668  # 
    67   #   GuestBookController.template_root = "templates/" 
    68   #   GuestBookController.process_cgi 
    69   # 
    70   # All actions assume that you want to render a template matching the name of the action at the end of the performance 
    71   # unless you tell it otherwise. The index action complies with this assumption, so after populating the @entries instance 
    72   # variable, the GuestBookController will render "templates/guestbook/index.rhtml". 
    73   # 
    74   # Unlike index, the sign action isn't interested in rendering a template. So after performing its main purpose (creating a  
    75   # new entry in the guest book), it sheds the rendering assumption and initiates a redirect instead. This redirect works by 
    76   # returning an external "302 Moved" HTTP response that takes the user to the index action. 
     69  # Actions, by default, render a template in the <tt>app/views</tt> directory corresponding to the name of the controller and action 
     70  # after executing code in the action. For example, the +index+ action of the +GuestBookController+  would render the  
     71  # template <tt>app/views/guestbook/index.rhtml</tt> by default after populating the <tt>@entries</tt> instance variable. 
     72  # 
     73  # Unlike index, the sign action will not render a template. After performing its main purpose (creating a  
     74  # new entry in the guest book), it initiates a redirect instead. This redirect works by returning an external  
     75  # "302 Moved" HTTP response that takes the user to the index action. 
    7776  # 
    7877  # The index and sign represent the two basic action archetypes used in Action Controllers. Get-and-show and do-and-redirect. 
    7978  # Most actions are variations of these themes. 
    80   # 
    81   # Also note that it's the final call to <tt>process_cgi</tt> that actually initiates the action performance. It will extract 
    82   # request and response objects from the CGI 
    83   # 
    84   # When Action Pack is used inside of Rails, the template_root is automatically configured and you don't need to call process_cgi 
    85   # yourself. 
    8679  # 
    8780  # == Requests 
     
    10295  # == Parameters 
    10396  # 
    104   # All request parameters, whether they come from a GET or POST request, or from the URL, are available through the params hash. 
    105   # So an action that was performed through /weblog/list?category=All&limit=5 will include { "category" => "All", "limit" => 5 } 
    106   # in params. 
     97  # All request parameters, whether they come from a GET or POST request, or from the URL, are available through the params method 
     98  # which returns a hash. For example, an action that was performed through <tt>/weblog/list?category=All&limit=5</tt> will include  
     99  # <tt>{ "category" => "All", "limit" => 5 }</tt> in params. 
    107100  # 
    108101  # It's also possible to construct multi-dimensional parameter hashes by specifying keys using brackets, such as: 
     
    117110  # == Sessions 
    118111  # 
    119   # Sessions allows you to store objects in memory between requests. This is useful for objects that are not yet ready to be persisted, 
     112  # Sessions allows you to store objects in between requests. This is useful for objects that are not yet ready to be persisted, 
    120113  # 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 
    121114  # 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  
    122115  # they could be changed unknowingly. It's usually too much work to keep it all synchronized -- something databases already excel at. 
    123116  # 
    124   # You can place objects in the session by using the <tt>session</tt> hash accessor
     117  # You can place objects in the session by using the <tt>session</tt> method, which accesses a hash
    125118  # 
    126119  #   session[:person] = Person.authenticate(user_name, password) 
     
    129122  # 
    130123  #   Hello #{session[:person]} 
    131   # 
    132   # Any object can be placed in the session (as long as it can be Marshalled). But remember that 1000 active sessions each storing a 
    133   # 50kb object could lead to a 50MB memory overhead. In other words, think carefully about size and caching before resorting to the use 
    134   # of the session. 
    135124  # 
    136125  # For removing objects from the session, you can either assign a single key to nil, like <tt>session[:person] = nil</tt>, or you can 
    137126  # remove the entire session with reset_session. 
    138127  # 
     128  # By default, sessions are stored on the file system in <tt>RAILS_ROOT/tmp/sessions</tt>. Any object can be placed in the session  
     129  # (as long as it can be Marshalled). But remember that 1000 active sessions each storing a 50kb object could lead to a 50MB store on the filesystem. 
     130  # In other words, think carefully about size and caching before resorting to the use of the session on the filesystem. 
     131  # 
     132  # An alternative to storing sessions on disk is to use ActiveRecordStore to store sessions in your database, which can solve problems 
     133  # caused by storing sessions in the file system and may speed up your application. To use ActiveRecordStore, uncomment the line: 
     134  #    
     135  #   config.action_controller.session_store = :active_record_store 
     136  # 
     137  # in your <tt>environment.rb</tt> and run <tt>rake db:sessions:create</tt>. 
     138  # 
    139139  # == Responses 
    140140  # 
    141141  # Each action results in a response, which holds the headers and document to be sent to the user's browser. The actual response 
    142   # object is generated automatically through the use of renders and redirects, so it's normally nothing you'll need to be concerned about
     142  # object is generated automatically through the use of renders and redirects and requires no user intervention
    143143  # 
    144144  # == Renders 
     
    162162  #     @results = Search.find(params[:query]) 
    163163  #     case @results 
    164   #       when 0 then render :action=> "no_results" 
    165   #       when 1 then render :action=> "show" 
    166   #       when 2..10 then render :action=> "show_many" 
     164  #       when 0 then render :action => "no_results" 
     165  #       when 1 then render :action => "show" 
     166  #       when 2..10 then render :action => "show_many" 
    167167  #     end 
    168168  #   end 
     
    172172  # == Redirects 
    173173  # 
    174   # Redirecting is what actions that update the model do when they're done. The <tt>save_post</tt> method shouldn't be responsible for also 
    175   # showing the post once it's saved -- that's the job for <tt>show_post</tt>. So once <tt>save_post</tt> has completed its business, it'll 
    176   # redirect to <tt>show_post</tt>. All redirects are external, which means that when the user refreshes his browser, it's not going to save 
    177   # the post again, but rather just show it one more time. 
    178   #  
    179   # This sounds fairly simple, but the redirection is complicated by the quest for a phenomenon known as "pretty urls". Instead of accepting 
    180   # the dreadful being that is "weblog_controller?action=show&post_id=5", Action Controller goes out of its way to represent the former as 
    181   # "/weblog/show/5". And this is even the simple case. As an example of a more advanced pretty url consider 
    182   # "/library/books/ISBN/0743536703/show", which can be mapped to books_controller?action=show&type=ISBN&id=0743536703. 
    183   #  
    184   # Redirects work by rewriting the URL of the current action. So if the show action was called by "/library/books/ISBN/0743536703/show",  
    185   # we can redirect to an edit action simply by doing <tt>redirect_to(:action => "edit")</tt>, which could throw the user to  
    186   # "/library/books/ISBN/0743536703/edit". Naturally, you'll need to setup the routes configuration file to point to the proper controller 
    187   # and action in the first place, but once you have, it can be rewritten with ease. 
    188   #  
    189   # Let's consider a bunch of examples on how to go from "/clients/37signals/basecamp/project/dash" to somewhere else: 
    190   # 
    191   #   redirect_to(:action => "edit") => 
    192   #     /clients/37signals/basecamp/project/dash 
    193   #    
    194   #   redirect_to(:client_name => "nextangle", :project_name => "rails") => 
    195   #     /clients/nextangle/rails/project/dash 
    196   # 
    197   # Those redirects happen under the configuration of: 
    198   # 
    199   #   map.connect 'clients/:client_name/:project_name/:controller/:action' 
     174  # Redirects are used to move from one action to another. For example, after a <tt>create</tt> action, which stores a blog entry to a database, 
     175  # we might like to show the user the new entry. Because we're following good DRY principles (Don't Repeat Yourself), we're going to reuse (and redirect to) 
     176  # a <tt>show</tt> action that we'll assume has already been created. The code might look like this: 
     177  # 
     178  #   def create 
     179  #     @entry = Entry.new(params[:entry]) 
     180  #     if @entry.save 
     181  #       # The entry was saved correctly, redirect to show 
     182  #       redirect_to :action => 'show', :id => @entry.id 
     183  #     else 
     184  #       # things didn't go so well, do something else 
     185  #     end 
     186  #   end 
     187  # 
     188  # In this case, after saving our new entry to the database, the user is redirected to the <tt>show</tt> method which is then executed. 
    200189  # 
    201190  # == Calling multiple redirects or renders 
     
    215204  #   end 
    216205  # 
    217   # == Environments 
    218   # 
    219   # Action Controller works out of the box with CGI, FastCGI, and mod_ruby. CGI and mod_ruby controllers are triggered just the same using: 
    220   # 
    221   #   WeblogController.process_cgi 
    222   # 
    223   # FastCGI controllers are triggered using: 
    224   # 
    225   #   FCGI.each_cgi{ |cgi| WeblogController.process_cgi(cgi) } 
    226206  class Base 
    227207    DEFAULT_RENDER_STATUS_CODE = "200 OK" 
  • trunk/actionpack/Rakefile

    r4204 r4290  
    4747  rdoc.options << '--line-numbers' << '--inline-source' 
    4848  rdoc.template = "#{ENV['template']}.rb" if ENV['template'] 
    49   rdoc.rdoc_files.include('README', 'RUNNING_UNIT_TESTS', 'CHANGELOG') 
    50   rdoc.rdoc_files.include('lib/**/*.rb') 
     49  if ENV['DOC_FILES']  
     50    rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/)) 
     51  else 
     52    rdoc.rdoc_files.include('README', 'RUNNING_UNIT_TESTS', 'CHANGELOG') 
     53    rdoc.rdoc_files.include('lib/**/*.rb') 
     54  end 
    5155} 
    5256