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

Changeset 462

Show
Ignore:
Timestamp:
01/20/05 14:13:34 (4 years ago)
Author:
david
Message:

Fixed that all redirect and render calls now return true, so you can use the pattern of "do and return". Added that renders and redirects called in before_filters will have the same effect as returning false: stopping the chain. Added that only one render or redirect can happen per action. The first call wins and subsequent calls are ignored.

Files:

Legend:

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

    r458 r462  
     1*SVN* 
     2 
     3* Fixed that a default fragment store wan't being set to MemoryStore as intended. 
     4 
     5* Fixed that all redirect and render calls now return true, so you can use the pattern of "do and return". Example: 
     6 
     7    def show 
     8      redirect_to(:action => "login") and return unless @person.authenticated? 
     9      render_text "I won't happen unless the person is authenticated" 
     10    end 
     11 
     12* Added that renders and redirects called in before_filters will have the same effect as returning false: stopping the chain. Example: 
     13 
     14    class WeblogController 
     15      before_filter { |c| c.send(:redirect_to_url("http://www.farfaraway.com")}) } 
     16       
     17      def hello 
     18        render_text "I will never be called" 
     19      end 
     20    end 
     21 
     22 
     23* Added that only one render or redirect can happen per action. The first call wins and subsequent calls are ignored. Example: 
     24 
     25    def do_something 
     26      redirect_to :action => "elsewhere" 
     27      render_action "overthere" 
     28    end 
     29   
     30  Only the redirect happens. The rendering call is simply ignored. 
     31 
     32 
    133*1.3.1* (January 18th, 2005) 
    234 
  • trunk/actionpack/lib/action_controller/base.rb

    r427 r462  
    163163  # For more examples of redirecting options, have a look at the unit test in test/controller/url_test.rb. It's very readable and will give 
    164164  # you an excellent understanding of the different options and what they do. 
     165  # 
     166  # == Calling multiple redirects or renders 
     167  # 
     168  # The rule for handling calls of multiple redirects and renders is that the first call wins. So in the following example: 
     169  # 
     170  #   def do_something 
     171  #     redirect_to :action => "elsewhere" 
     172  #     render_action "overthere" 
     173  #   end 
     174  # 
     175  # Only the redirect happens. The rendering call is simply ignored. 
    165176  # 
    166177  # == Environments 
     
    386397      # Use block for response body if provided (useful for deferred rendering or streaming output). 
    387398      def render_text(text = nil, status = nil, &block) #:doc: 
     399        return if performed? 
    388400        add_variables_to_assigns 
    389401        @response.headers["Status"] = status || DEFAULT_RENDER_STATUS_CODE 
    390402        @response.body = block_given? ? block : text 
    391403        @performed_render = true 
    392         return false 
    393404      end 
    394405       
     
    542553      # second parameter and the browser will get "301 Moved Permanently" instead of "302 Found". 
    543554      def redirect_to_url(url, permanently = false) #:doc: 
     555        return if performed? 
    544556        logger.info("Redirected to #{url}") unless logger.nil? 
    545557        @response.redirect(url, permanently) 
    546558        @performed_redirect = true 
    547         return false 
    548559      end 
    549560 
     
    595606        if action_methods.include?(action_name) || action_methods.include?('method_missing') 
    596607          send(action_name) 
    597           render unless @performed_render || @performed_redirect 
     608          render unless performed? 
    598609        elsif template_exists? && template_public? 
    599610          render 
     
    601612          raise UnknownAction, "No action responded to #{action_name}", caller 
    602613        end 
     614      end 
     615       
     616      def performed? 
     617        @performed_render || @performed_redirect 
    603618      end 
    604619 
  • trunk/actionpack/lib/action_controller/filters.rb

    r354 r462  
    1313    # Filters have access to the request, response, and all the instance variables set by other filters in the chain 
    1414    # or by the action (in the case of after filters). Additionally, it's possible for a pre-processing <tt>before_filter</tt> 
    15     # to halt the processing before the intended action is processed by returning false. This is especially useful for 
    16     # filters like authentication where you're not interested in allowing the action to be performed if the proper  
    17     # credentials are not in order. 
     15    # to halt the processing before the intended action is processed by returning false or performing a redirect or render.  
     16    # This is especially useful for filters like authentication where you're not interested in allowing the action to be  
     17    # performed if the proper credentials are not in order. 
    1818    # 
    1919    # == Filter inheritance 
     
    291291 
    292292      def perform_action_with_filters 
    293         return if before_action == false 
     293        return if before_action == false || performed? 
    294294        perform_action_without_filters 
    295295        after_action 
  • trunk/actionpack/test/controller/filters_test.rb

    r354 r462  
    1313        @ran_filter ||= [] 
    1414        @ran_filter << "ensure_login" 
     15      end 
     16  end 
     17 
     18  class RenderingController < ActionController::Base 
     19    before_filter :render_something_else 
     20 
     21    def show 
     22      @ran_action = true 
     23      render_text "ran action" 
     24    end 
     25 
     26    private 
     27      def render_something_else 
     28        render_text "something else" 
    1529      end 
    1630  end 
     
    265279                 MixedFilterController.execution_log 
    266280  end 
     281   
     282  def test_rendering_breaks_filtering_chain 
     283    response = test_process(RenderingController) 
     284    assert_equal "something else", response.body 
     285    assert !response.template.assigns["ran_action"] 
     286  end 
    267287 
    268288  private