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

Changeset 5131

Show
Ignore:
Timestamp:
09/17/06 18:10:25 (2 years ago)
Author:
david
Message:

Added that respond_to blocks will automatically set the content type to be the same as is requested [DHH]

Files:

Legend:

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

    r5129 r5131  
    11*SVN* 
     2 
     3* Added that respond_to blocks will automatically set the content type to be the same as is requested [DHH]. Examples: 
     4 
     5    respond_to do |format| 
     6      format.html { render :text => "I'm being sent as text/html" } 
     7      format.rss  { render :text => "I'm being sent as application/rss+xml" } 
     8      format.atom { render :text => "I'm being sent as application/xml", :content_type => Mime::XML } 
     9    end 
    210 
    311* Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) [DHH] 
  • trunk/actionpack/lib/action_controller/mime_responds.rb

    r4953 r5131  
    109109    class Responder #:nodoc: 
    110110      DEFAULT_BLOCKS = [:html, :js, :xml].inject({}) do |blocks, ext| 
    111         blocks.update ext => %(Proc.new { render :action => "\#{action_name}.r#{ext}" }) 
     111        blocks.update ext => %(Proc.new { render :action => "\#{action_name}.r#{ext}", :content_type => Mime::#{ext.to_s.upcase} }) 
    112112      end       
    113113       
     
    130130         
    131131        if block_given? 
    132           @responses[mime_type] = block 
     132          @responses[mime_type] = Proc.new do 
     133            eval "response.content_type = Mime::#{mime_type.to_sym.to_s.upcase}", @block_binding 
     134            block.call 
     135          end 
    133136        else 
    134137          if source = DEFAULT_BLOCKS[mime_type.to_sym] 
  • trunk/actionpack/test/controller/content_type_test.rb

    r5129 r5131  
    2929    response.content_type = Mime::HTML 
    3030    render :action => "render_default_for_rxml" 
     31  end 
     32 
     33  def render_default_content_types_for_respond_to 
     34    respond_to do |format| 
     35      format.html { render :text   => "hello world!" } 
     36      format.xml  { render :action => "render_default_content_types_for_respond_to.rhtml" } 
     37      format.js   { render :text   => "hello world!" } 
     38      format.rss  { render :text   => "hello world!", :content_type => Mime::XML } 
     39    end 
    3140  end 
    3241 
     
    97106    assert_equal "utf-8", @response.charset     
    98107  end 
     108   
     109  def test_render_default_content_types_for_respond_to 
     110    @request.env["HTTP_ACCEPT"] = Mime::HTML.to_s 
     111    get :render_default_content_types_for_respond_to 
     112    assert_equal Mime::HTML, @response.content_type 
     113 
     114    @request.env["HTTP_ACCEPT"] = Mime::JS.to_s 
     115    get :render_default_content_types_for_respond_to 
     116    assert_equal Mime::JS, @response.content_type 
     117  end 
     118 
     119  def test_render_default_content_types_for_respond_to_with_template 
     120    @request.env["HTTP_ACCEPT"] = Mime::XML.to_s 
     121    get :render_default_content_types_for_respond_to 
     122    assert_equal Mime::XML, @response.content_type 
     123  end 
     124   
     125  def test_render_default_content_types_for_respond_to_with_overwrite 
     126    @request.env["HTTP_ACCEPT"] = Mime::RSS.to_s 
     127    get :render_default_content_types_for_respond_to 
     128    assert_equal Mime::XML, @response.content_type 
     129  end 
    99130end