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

Changeset 3838

Show
Ignore:
Timestamp:
03/11/06 01:23:29 (3 years ago)
Author:
david
Message:

Added better support for using the same actions to output for different sources depending on the Accept header [DHH] Added Base#render(:xml => xml) that works just like Base#render(:text => text), but sets the content-type to text/xml and the charset to UTF-8 [DHH]

Files:

Legend:

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

    r3819 r3838  
    11*SVN* 
     2 
     3* Added better support for using the same actions to output for different sources depending on the Accept header [DHH]. Example: 
     4 
     5    class WeblogController < ActionController::Base 
     6      def create 
     7        @post = Post.create(params[:post]) 
     8       
     9        respond_to do |type| 
     10          type.js   { render }  # renders create.rjs 
     11          type.html { redirect_to :action => "index" } 
     12          type.xml  do 
     13            headers["Location"] = url_for(:action => "show", :id => @post) 
     14            render(:nothing, :status => "201 Created") 
     15          end 
     16        end 
     17      end 
     18    end 
     19 
     20* Added Base#render(:xml => xml) that works just like Base#render(:text => text), but sets the content-type to text/xml and the charset to UTF-8 [DHH] 
    221 
    322* Integration test's url_for now runs in the context of the last request (if any) so after post /products/show/1 url_for :action => 'new' will yield /product/new  [Tobias Luetke] 
  • trunk/actionpack/lib/action_controller.rb

    r3777 r3838  
    4545require 'action_controller/layout' 
    4646require 'action_controller/dependencies' 
     47require 'action_controller/mime_responds' 
    4748require 'action_controller/pagination' 
    4849require 'action_controller/scaffolding' 
     
    6869  include ActionController::Rescue 
    6970  include ActionController::Dependencies 
     71  include ActionController::MimeResponds 
    7072  include ActionController::Pagination 
    7173  include ActionController::Scaffolding 
  • trunk/actionpack/lib/action_controller/base.rb

    r3806 r3838  
     1require 'action_controller/mime_type' 
    12require 'action_controller/request' 
    23require 'action_controller/response' 
     
    649650          elsif action_name = options[:action] 
    650651            render_action(action_name, options[:status], options[:layout])  
    651              
     652 
     653          elsif xml = options[:xml] 
     654            render_xml(xml, options[:status]) 
     655 
    652656          elsif partial = options[:partial] 
    653657            partial = default_template_name if partial == true 
     
    716720 
    717721      def render_javascript(javascript, status = nil) 
    718         @response.headers['Content-Type'] = 'text/javascript
     722        @response.headers['Content-Type'] = 'text/javascript; charset=UTF-8
    719723        render_text(javascript, status) 
     724      end 
     725 
     726      def render_xml(xml, status = nil) 
     727        @response.headers['Content-Type'] = 'text/xml; charset=UTF-8' 
     728        render_text(xml, status) 
    720729      end 
    721730 
  • trunk/actionpack/lib/action_controller/request.rb

    r3808 r3838  
    5555      if @env['HTTP_X_POST_DATA_FORMAT']           
    5656        case @env['HTTP_X_POST_DATA_FORMAT'].downcase.to_sym 
    57         when :yaml 
    58           @content_type = 'application/x-yaml' 
    59         when :xml 
    60           @content_type = 'application/xml' 
    61         end 
    62       end 
    63       @content_type 
     57          when :yaml 
     58            @content_type = 'application/x-yaml' 
     59          when :xml 
     60            @content_type = 'application/xml' 
     61          end 
     62      end 
     63 
     64      @content_type = Mime::Type.new(@content_type) 
     65    end 
     66 
     67    def accepts 
     68      @accepts ||= (@env['HTTP_ACCEPT'].strip.blank? ? "*/*" : @env['HTTP_ACCEPT']).split(";").collect! do |mime_type|  
     69        Mime::Type.new(mime_type.strip) 
     70      end 
    6471    end 
    6572 
  • trunk/actionpack/lib/action_controller/test_process.rb

    r3820 r3838  
    105105      self.query_parameters   = {} 
    106106      self.path_parameters    = {} 
    107       @request_method = nil 
     107      @request_method, @accepts, @content_type = nil, nil, nil 
    108108    end     
    109109 
  • trunk/actionpack/lib/action_view/helpers/javascripts/prototype.js

    r3795 r3838  
    704704    var requestHeaders = 
    705705      ['X-Requested-With', 'XMLHttpRequest', 
    706        'X-Prototype-Version', Prototype.Version]; 
     706       'X-Prototype-Version', Prototype.Version, 
     707       'Accept', 'text/javascript; text/html; text/xml; */*' ]; 
    707708 
    708709    if (this.options.method == 'post') { 
  • trunk/actionpack/test/controller/new_render_test.rb

    r3814 r3838  
    545545    get :update_page 
    546546    assert_template nil 
    547     assert_equal 'text/javascript', @response.headers['Content-Type'] 
     547    assert_equal 'text/javascript; charset=UTF-8', @response.headers['Content-Type'] 
    548548    assert_equal 2, @response.body.split($/).length 
    549549  end 
     
    552552    get :update_page_with_instance_variables 
    553553    assert_template nil 
    554     assert_equal 'text/javascript', @response.headers['Content-Type'] 
     554    assert_equal 'text/javascript; charset=UTF-8', @response.headers['Content-Type'] 
    555555    assert_match /balance/, @response.body 
    556556    assert_match /\$37/, @response.body 
  • trunk/actionpack/test/controller/webservice_test.rb

    r3778 r3838  
    7272 
    7373  def test_register_and_use_xml_simple 
    74     ActionController::Base.param_parsers['application/xml'] = :xml_simple 
     74    ActionController::Base.param_parsers['application/xml'] = Proc.new { |data| XmlSimple.xml_in(data, 'ForceArray' => false) } 
    7575    process('POST', 'application/xml', '<request><summary>content...</summary><title>SimpleXml</title></request>' ) 
    7676    assert_equal 'summary, title', @controller.response.body