Permalink
Browse files

Added :location option to render so that the common pattern of render…

…ing 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
  • Loading branch information...
dhh committed Apr 24, 2007
1 parent 57352f8 commit 5208d8d8173fc5ac47a061d872074d0fd797d70c
Showing with 21 additions and 8 deletions.
  1. +4 −0 actionpack/CHANGELOG
  2. +7 −8 actionpack/lib/action_controller/base.rb
  3. +10 −0 actionpack/test/controller/render_test.rb
View
@@ -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]
@@ -743,16 +743,11 @@ def view_paths
# 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 @@ def render(options = nil, deprecated_status = nil, &block) #:doc:
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])
@@ -73,6 +73,10 @@ def heading
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 @@ def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
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)

0 comments on commit 5208d8d

Please sign in to comment.