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

Ticket #9953: default_render_patch.diff

File default_render_patch.diff, 3.8 kB (added by cjheath, 11 months ago)
  • actionpack/test/controller/render_test.rb

    old new  
    152152    end 
    153153  end 
    154154 
     155  def default_render 
     156    if @alternate_default_render 
     157      @alternate_default_render.call 
     158    else 
     159      render 
     160    end 
     161  end 
     162 
     163  def render_alternate_default 
     164    # For this test, the method "default_render" is overridden: 
     165    @alternate_default_render = lambda { 
     166        render :update do |page| 
     167          page.replace :foo, :partial => 'partial' 
     168        end 
     169      } 
     170  end 
     171 
    155172  def rescue_action(e) raise end 
    156173 
    157174  private 
     
    413430    assert_equal 'partial js', @response.body 
    414431  end 
    415432 
     433  def test_should_render_with_alternate_default_render 
     434    xhr :get, :render_alternate_default 
     435    assert_equal %(Element.replace("foo", "partial html");), @response.body 
     436  end 
     437 
    416438  protected 
    417439   
    418440    def etag_for(text) 
  • actionpack/lib/action_controller/base.rb

    old new  
    7070  end 
    7171 
    7272  class DoubleRenderError < ActionControllerError #:nodoc: 
    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 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\"." 
     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\". 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\"." 
    7474 
    7575    def initialize(message = nil) 
    7676      super(message || DEFAULT_MESSAGE) 
     
    226226  # 
    227227  # == Calling multiple redirects or renders 
    228228  # 
    229   # An action should conclude with a single render or redirect. Attempting to try to do either again will result in a DoubleRenderError: 
     229  # An action may contain only a single render or a single redirect. Attempting to try to do either again will result in a DoubleRenderError: 
    230230  # 
    231231  #   def do_something 
    232232  #     redirect_to :action => "elsewhere" 
     
    240240  #     render :action => "overthere" # won't be called unless monkeys is nil 
    241241  #   end 
    242242  # 
     243  # == Default render 
     244  # 
     245  # If you don't call either render or redirect, a default render will occur. 
     246  # 
    243247  class Base 
    244248    DEFAULT_RENDER_STATUS_CODE = "200 OK" 
    245249 
     
    11251129        end 
    11261130      end 
    11271131 
     1132      def default_render 
     1133        render 
     1134      end 
     1135 
    11281136      def perform_action 
    11291137        if self.class.action_methods.include?(action_name) 
    11301138          send(action_name) 
    1131           render unless performed? 
     1139          default_render unless performed? 
    11321140        elsif respond_to? :method_missing 
    11331141          method_missing action_name 
    1134           render unless performed? 
     1142          default_render unless performed? 
    11351143        elsif template_exists? && template_public? 
    1136           render 
     1144          default_render 
    11371145        else 
    11381146          raise UnknownAction, "No action responded to #{action_name}", caller 
    11391147        end