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

Changeset 4384

Show
Ignore:
Timestamp:
05/31/06 18:52:24 (2 years ago)
Author:
david
Message:

Added interrogation of params[:format] to determine Accept type. If :format is specified and matches a declared extension, like "rss" or "xml", that mime type will be put in front of the accept handler. This means you can link to the same action from different extensions and use that fact to determine output [DHH]

Files:

Legend:

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

    r4375 r4384  
    11*SVN* 
     2 
     3* Added interrogation of params[:format] to determine Accept type. If :format is specified and matches a declared extension, like "rss" or "xml", that mime type will be put in front of the accept handler. This means you can link to the same action from different extensions and use that fact to determine output [DHH]. Example: 
     4 
     5  class WeblogController < ActionController::Base 
     6    def index 
     7      @posts = Post.find :all 
     8       
     9      respond_to do |format| 
     10        format.html 
     11        format.xml { render :xml => @posts.to_xml } 
     12        format.rss { render :action => "feed.rxml" } 
     13      end 
     14    end 
     15  end 
     16   
     17  # returns HTML when requested by a browser, since the browser 
     18  # has the HTML mimetype at the top of its priority list 
     19  Accept: text/html 
     20  GET /weblog  
     21   
     22  # returns the XML  
     23  Accept: application/xml 
     24  GET /weblog  
     25 
     26  # returns the HTML  
     27  Accept: application/xml 
     28  GET /weblog.html 
     29 
     30  # returns the XML 
     31  Accept: text/html 
     32  GET /weblog.xml 
     33   
     34  All this relies on the fact that you have a route that includes .:format. 
     35   
    236 
    337* Expanded :method option in FormTagHelper#form_tag, FormHelper#form_for, PrototypeHelper#remote_form_for, PrototypeHelper#remote_form_tag, and PrototypeHelper#link_to_remote to allow for verbs other than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post [DHH] 
  • trunk/actionpack/lib/action_controller/mime_responds.rb

    r4083 r4384  
    111111      def initialize(block_binding) 
    112112        @block_binding = block_binding 
    113         @mime_type_priority = eval("request.accepts", block_binding) 
     113        @mime_type_priority = eval( 
     114          "(params[:format] && Mime::EXTENSION_LOOKUP[params[:format]]) ? " + 
     115          "[ Mime::EXTENSION_LOOKUP[params[:format]] ] : request.accepts",  
     116          block_binding 
     117        ) 
     118 
    114119        @order     = [] 
    115120        @responses = {} 
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r4079 r4384  
    120120  YAML  = Type.new "application/x-yaml", :yaml, %w( text/yaml ) 
    121121 
     122 
    122123  LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) } 
    123124 
     
    140141  LOOKUP["application/rss+xml"]      = RSS 
    141142  LOOKUP["application/atom+xml"]     = ATOM 
     143 
     144   
     145  EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) } 
     146 
     147  EXTENSION_LOOKUP["html"]  = HTML 
     148  EXTENSION_LOOKUP["xhtml"] = HTML 
     149 
     150  EXTENSION_LOOKUP["xml"]   = XML 
     151 
     152  EXTENSION_LOOKUP["js"]    = JS 
     153 
     154  EXTENSION_LOOKUP["yml"]   = YAML 
     155  EXTENSION_LOOKUP["yaml"]  = YAML 
     156 
     157  EXTENSION_LOOKUP["rss"]   = RSS 
     158  EXTENSION_LOOKUP["atom"]  = ATOM 
    142159end 
  • trunk/actionpack/test/controller/mime_responds_test.rb

    r3948 r4384  
    255255    assert_equal '$("body").visualEffect("highlight");', @response.body 
    256256  end 
     257   
     258  def test_forced_format 
     259    get :html_xml_or_rss 
     260    assert_equal "HTML", @response.body 
     261 
     262    get :html_xml_or_rss, :format => "html" 
     263    assert_equal "HTML", @response.body 
     264 
     265    get :html_xml_or_rss, :format => "xml" 
     266    assert_equal "XML", @response.body 
     267 
     268    get :html_xml_or_rss, :format => "rss" 
     269    assert_equal "RSS", @response.body 
     270  end 
    257271end