aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-04-24 17:52:03 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-04-24 17:52:03 +0000
commit5208d8d8173fc5ac47a061d872074d0fd797d70c (patch)
treebdf898db1b38fa17cc480be945b3eaa6bf0dddfc
parent57352f86d43f6d3ee30f07d795087e68bf07f521 (diff)
downloadrails-5208d8d8173fc5ac47a061d872074d0fd797d70c.tar.gz
rails-5208d8d8173fc5ac47a061d872074d0fd797d70c.tar.bz2
rails-5208d8d8173fc5ac47a061d872074d0fd797d70c.zip
Added :location option to render so that the common pattern of rendering a response after creating a new resource is now a 1-liner [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6572 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG4
-rwxr-xr-xactionpack/lib/action_controller/base.rb15
-rw-r--r--actionpack/test/controller/render_test.rb10
3 files changed, 21 insertions, 8 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 3cc848ebf5..040d1162ad 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,9 @@
*SVN*
+* Added :location option to render so that the common pattern of rendering a response after creating a new resource is now a 1-liner [DHH]
+
+ render :xml => post.to_xml, :status => :created, :location => post_url(post)
+
* Ensure that render_text only adds string content to the body of the response [DHH]
* Return the string representation from an Xml Builder when rendering a partial. Closes #5044 [tpope]
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index ea9b54dac9..c0c91a3ac9 100755
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -743,16 +743,11 @@ module ActionController #:nodoc:
# page.visual_effect :highlight, 'user_list'
# end
#
- # === Rendering nothing
+ # === Rendering with status and location headers
#
- # Rendering nothing is often convenient in combination with Ajax calls that perform their effect client-side or
- # when you just want to communicate a status code. Due to a bug in Safari, nothing actually means a single space.
+ # All renders take the :status and :location options and turn them into headers. They can even be used together:
#
- # # Renders an empty response with status code 200
- # render :nothing => true
- #
- # # Renders an empty response with status code 401 (access denied)
- # render :nothing => true, :status => 401
+ # render :xml => post.to_xml, :status => :created, :location => post_url(post)
def render(options = nil, deprecated_status = nil, &block) #:doc:
raise DoubleRenderError, "Can only render or redirect once per action" if performed?
@@ -779,6 +774,10 @@ module ActionController #:nodoc:
response.content_type = content_type.to_s
end
+ if location = options[:location]
+ response.headers["Location"] = location
+ end
+
if text = options[:text]
render_text(text, options[:status])
diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb
index cba21fc4cb..eb23ed0c5b 100644
--- a/actionpack/test/controller/render_test.rb
+++ b/actionpack/test/controller/render_test.rb
@@ -73,6 +73,10 @@ class TestController < ActionController::Base
head :ok
end
+ def location
+ render :xml => "<hello/>", :location => "http://example.com", :status => 201
+ end
+
def greeting
# let's just rely on the template
end
@@ -368,6 +372,12 @@ class RenderTest < Test::Unit::TestCase
assert_equal '<test>passed formatted html erb</test>', @response.body
end
+ def test_rendering_with_location_should_set_header
+ get :location
+ assert_equal "http://example.com", @response.headers["Location"]
+ end
+
+
protected
def assert_deprecated_render(&block)
assert_deprecated(/render/, &block)