1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
David Heinemeier Hansson 2006-05-31 18:52:24 +00:00
parent e7fe1c47ba
commit f9cd92f4ee
4 changed files with 71 additions and 1 deletions

View file

@ -1,5 +1,39 @@
*SVN* *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] * 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] * 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]

View file

@ -110,7 +110,12 @@ module ActionController #:nodoc:
def initialize(block_binding) def initialize(block_binding)
@block_binding = 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 = [] @order = []
@responses = {} @responses = {}
end end

View file

@ -119,6 +119,7 @@ module Mime
ATOM = Type.new "application/atom+xml", :atom ATOM = Type.new "application/atom+xml", :atom
YAML = Type.new "application/x-yaml", :yaml, %w( text/yaml ) YAML = Type.new "application/x-yaml", :yaml, %w( text/yaml )
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) } LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) }
LOOKUP["*/*"] = ALL LOOKUP["*/*"] = ALL
@ -139,4 +140,20 @@ module Mime
LOOKUP["application/rss+xml"] = RSS LOOKUP["application/rss+xml"] = RSS
LOOKUP["application/atom+xml"] = ATOM 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 end

View file

@ -254,4 +254,18 @@ class MimeControllerTest < Test::Unit::TestCase
xhr :get, :using_defaults xhr :get, :using_defaults
assert_equal '$("body").visualEffect("highlight");', @response.body assert_equal '$("body").visualEffect("highlight");', @response.body
end 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 end