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

Changeset 3441

Show
Ignore:
Timestamp:
01/20/06 22:04:07 (3 years ago)
Author:
sam
Message:

Add render :update for inline RJS

Files:

Legend:

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

    r3438 r3441  
    11*SVN* 
     2 
     3* Add render :update for inline RJS. [Sam Stephenson]  Example: 
     4  class UserController < ApplicationController 
     5    def refresh 
     6      render :update do |page| 
     7        page.replace_html  'user_list', :partial => 'user', :collection => @users 
     8        page.visual_effect :highlight, 'user_list' 
     9      end 
     10    end 
     11  end 
    212 
    313* allow nil objects for error_messages_for [Michael Koziarski] 
  • trunk/actionpack/lib/action_controller/base.rb

    r3406 r3441  
    369369      # Returns a URL that has been rewritten according to the options hash and the defined Routes.  
    370370      # (For doing a complete redirect, use redirect_to). 
    371       #   
     371      #   
    372372      # <tt>url_for</tt> is used to: 
    373       #   
     373      #   
    374374      # All keys given to url_for are forwarded to the Route module, save for the following: 
    375375      # * <tt>:anchor</tt> -- specifies the anchor name to be appended to the path. For example,  
     
    387387      # The default Routes setup supports a typical Rails path of "controller/action/id" where action and id are optional, with 
    388388      # action defaulting to 'index' when not given. Here are some typical url_for statements and their corresponding URLs: 
    389       #   
     389      #   
    390390      #   url_for :controller => 'posts', :action => 'recent' # => 'proto://host.com/posts/recent' 
    391391      #   url_for :controller => 'posts', :action => 'index' # => 'proto://host.com/posts' 
     
    396396      # other parameters, including <tt>:controller</tt>, <tt>:id</tt>, and any other parameters that are placed into a Route's 
    397397      # path. 
    398       #   
     398      #   
    399399      # The URL helpers such as <tt>url_for</tt> have a limited form of memory: when generating a new URL, they can look for 
    400400      # missing values in the current request's parameters. Routes attempts to guess when a value should and should not be 
     
    419419      # value that appears in the slot for <tt>:first</tt> is not equal to default value for <tt>:first</tt> we stop using 
    420420      # defaults. On it's own, this rule can account for much of the typical Rails URL behavior. 
    421       #   
     421      #   
    422422      # Although a convienence, defaults can occasionaly get in your way. In some cases a default persists longer than desired. 
    423423      # The default may be cleared by adding <tt>:name => nil</tt> to <tt>url_for</tt>'s options. 
     
    572572      # _Deprecation_ _notice_: This used to have the signature <tt>render_template(template, status = 200, type = :rhtml)</tt> 
    573573      # 
     574      # === Rendering inline JavaScriptGenerator page updates 
     575      # 
     576      # In addition to rendering JavaScriptGenerator page updates with Ajax in RJS templates (see ActionView::Base for details), 
     577      # you can also pass the <tt>:update</tt> parameter to +render+, along with a block, to render page updates inline. 
     578      # 
     579      #   render :update do |page| 
     580      #     page.replace_html  'user_list', :partial => 'user', :collection => @users 
     581      #     page.visual_effect :highlight, 'user_list' 
     582      #   end 
     583      # 
    574584      # === Rendering nothing 
    575585      # 
     
    582592      #   # Renders an empty response with status code 401 (access denied) 
    583593      #   render :nothing => true, :status => 401 
    584       def render(options = nil, deprecated_status = nil) #:doc: 
     594      def render(options = nil, deprecated_status = nil, &block) #:doc: 
    585595        raise DoubleRenderError, "Can only render or redirect once per action" if performed? 
    586596 
    587597        # Backwards compatibility 
    588598        unless options.is_a?(Hash) 
    589           return render_file(options || default_template_name, deprecated_status, true) 
     599          if options == :update 
     600            options = {:update => true} 
     601          else 
     602            return render_file(options || default_template_name, deprecated_status, true) 
     603          end 
    590604        end 
    591605 
     
    614628            end 
    615629 
     630          elsif options[:update] 
     631            generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block) 
     632            render_javascript(generator.to_s) 
     633 
    616634          elsif options[:nothing] 
    617635            # Safari doesn't pass the headers of the return if the response is zero length 
     
    660678        @response.headers['Status'] = (status || DEFAULT_RENDER_STATUS_CODE).to_s 
    661679        @response.body = text 
     680      end 
     681 
     682      def render_javascript(javascript, status = nil) 
     683        @response.headers['Content-type'] = 'text/javascript' 
     684        render_text(javascript, status) 
    662685      end 
    663686 
  • trunk/actionpack/lib/action_controller/benchmarking.rb

    r2401 r3441  
    4444    end 
    4545 
    46     def render_with_benchmark(options = nil, deprecated_status = nil
     46    def render_with_benchmark(options = nil, deprecated_status = nil, &block
    4747      unless logger 
    48         render_without_benchmark(options, deprecated_status
     48        render_without_benchmark(options, deprecated_status, &block
    4949      else 
    5050        db_runtime = ActiveRecord::Base.connection.reset_runtime if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected? 
    5151 
    5252        render_output = nil 
    53         @rendering_runtime = Benchmark::measure{ render_output = render_without_benchmark(options, deprecated_status) }.real 
     53        @rendering_runtime = Benchmark::measure{ render_output = render_without_benchmark(options, deprecated_status, &block) }.real 
    5454 
    5555        if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected? 
  • trunk/actionpack/lib/action_controller/layout.rb

    r3423 r3441  
    212212    end 
    213213 
    214     def render_with_a_layout(options = nil, deprecated_status = nil, deprecated_layout = nil) #:nodoc: 
     214    def render_with_a_layout(options = nil, deprecated_status = nil, deprecated_layout = nil, &block) #:nodoc: 
    215215      template_with_options = options.is_a?(Hash) 
    216216 
     
    220220 
    221221        if template_with_options 
    222           content_for_layout = render_with_no_layout(options
     222          content_for_layout = render_with_no_layout(options, &block
    223223          deprecated_status = options[:status] || deprecated_status 
    224224        else 
    225           content_for_layout = render_with_no_layout(options, deprecated_status
     225          content_for_layout = render_with_no_layout(options, deprecated_status, &block
    226226        end 
    227227 
     
    231231        render_text(@template.render_file(layout, true), deprecated_status) 
    232232      else 
    233         render_with_no_layout(options, deprecated_status
     233        render_with_no_layout(options, deprecated_status, &block
    234234      end 
    235235    end 
  • trunk/actionpack/test/controller/new_render_test.rb

    r3321 r3441  
    187187    @project_id = 4 
    188188    render :action => 'delete_with_js' 
     189  end 
     190 
     191  def update_page 
     192    render :update do |page| 
     193      page.replace_html 'balance', '$37,000,000.00' 
     194      page.visual_effect :highlight, 'balance' 
     195    end 
    189196  end 
    190197 
     
    481488  end 
    482489   
     490  def test_update_page 
     491    get :update_page 
     492    assert_equal 'text/javascript', @response.headers['Content-type'] 
     493    assert_equal 2, @response.body.split($/).length 
     494  end 
     495   
    483496  def test_yield_content_for 
    484497    get :yield_content_for