Permalink
Browse files

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]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4384 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed May 31, 2006
1 parent e7fe1c4 commit f9cd92f4ee49c689f25dbe7da008fb298a5feb4f
View
@@ -1,5 +1,39 @@
*SVN*
+* 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:
+
+ class WeblogController < ActionController::Base
+ def index
+ @posts = Post.find :all
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @posts.to_xml }
+ format.rss { render :action => "feed.rxml" }
+ end
+ end
+ end
+
+ # returns HTML when requested by a browser, since the browser
+ # has the HTML mimetype at the top of its priority list
+ Accept: text/html
+ GET /weblog
+
+ # returns the XML
+ Accept: application/xml
+ GET /weblog
+
+ # returns the HTML
+ Accept: application/xml
+ GET /weblog.html
+
+ # returns the XML
+ Accept: text/html
+ GET /weblog.xml
+
+ All this relies on the fact that you have a route that includes .:format.
+
+
* 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]
* Added :method option to UrlHelper#link_to, which allows for using other verbs than GET for the link. This replaces the :post option, which is now deprecated. Example: link_to "Destroy", person_url(:id => person), :method => :delete [DHH]
@@ -110,7 +110,12 @@ class Responder #:nodoc:
def initialize(block_binding)
@block_binding = block_binding
- @mime_type_priority = eval("request.accepts", block_binding)
+ @mime_type_priority = eval(
+ "(params[:format] && Mime::EXTENSION_LOOKUP[params[:format]]) ? " +
+ "[ Mime::EXTENSION_LOOKUP[params[:format]] ] : request.accepts",
+ block_binding
+ )
+
@order = []
@responses = {}
end
@@ -119,6 +119,7 @@ def ==(mime_type)
ATOM = Type.new "application/atom+xml", :atom
YAML = Type.new "application/x-yaml", :yaml, %w( text/yaml )
+
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) }
LOOKUP["*/*"] = ALL
@@ -139,4 +140,20 @@ def ==(mime_type)
LOOKUP["application/rss+xml"] = RSS
LOOKUP["application/atom+xml"] = ATOM
+
+
+ EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) }
+
+ EXTENSION_LOOKUP["html"] = HTML
+ EXTENSION_LOOKUP["xhtml"] = HTML
+
+ EXTENSION_LOOKUP["xml"] = XML
+
+ EXTENSION_LOOKUP["js"] = JS
+
+ EXTENSION_LOOKUP["yml"] = YAML
+ EXTENSION_LOOKUP["yaml"] = YAML
+
+ EXTENSION_LOOKUP["rss"] = RSS
+ EXTENSION_LOOKUP["atom"] = ATOM
end
@@ -254,4 +254,18 @@ def test_xhr
xhr :get, :using_defaults
assert_equal '$("body").visualEffect("highlight");', @response.body
end
+
+ def test_forced_format
+ get :html_xml_or_rss
+ assert_equal "HTML", @response.body
+
+ get :html_xml_or_rss, :format => "html"
+ assert_equal "HTML", @response.body
+
+ get :html_xml_or_rss, :format => "xml"
+ assert_equal "XML", @response.body
+
+ get :html_xml_or_rss, :format => "rss"
+ assert_equal "RSS", @response.body
+ end
end

0 comments on commit f9cd92f

Please sign in to comment.