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

Changeset 6572

Show
Ignore:
Timestamp:
04/24/07 17:52:03 (2 years ago)
Author:
david
Message:

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]

Files:

Legend:

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

    r6547 r6572  
    11*SVN* 
     2 
     3* 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] 
     4 
     5    render :xml => post.to_xml, :status => :created, :location => post_url(post) 
    26 
    37* Ensure that render_text only adds string content to the body of the response [DHH] 
  • trunk/actionpack/lib/action_controller/base.rb

    r6560 r6572  
    744744      #   end 
    745745      # 
    746       # === Rendering nothing 
    747       # 
    748       # Rendering nothing is often convenient in combination with Ajax calls that perform their effect client-side or 
    749       # when you just want to communicate a status code. Due to a bug in Safari, nothing actually means a single space. 
    750       # 
    751       #   # Renders an empty response with status code 200 
    752       #   render :nothing => true 
    753       # 
    754       #   # Renders an empty response with status code 401 (access denied) 
    755       #   render :nothing => true, :status => 401 
     746      # === Rendering with status and location headers 
     747      # 
     748      # All renders take the :status and :location options and turn them into headers. They can even be used together: 
     749      # 
     750      #   render :xml => post.to_xml, :status => :created, :location => post_url(post) 
    756751      def render(options = nil, deprecated_status = nil, &block) #:doc: 
    757752        raise DoubleRenderError, "Can only render or redirect once per action" if performed? 
     
    778773        if content_type = options[:content_type] 
    779774          response.content_type = content_type.to_s 
     775        end 
     776 
     777        if location = options[:location] 
     778          response.headers["Location"] = location 
    780779        end 
    781780 
  • trunk/actionpack/test/controller/render_test.rb

    r6517 r6572  
    7272  def heading 
    7373    head :ok 
     74  end 
     75 
     76  def location 
     77    render :xml => "<hello/>", :location => "http://example.com", :status => 201 
    7478  end 
    7579 
     
    369373  end 
    370374 
     375  def test_rendering_with_location_should_set_header 
     376    get :location 
     377    assert_equal "http://example.com", @response.headers["Location"] 
     378  end 
     379 
     380 
    371381  protected 
    372382    def assert_deprecated_render(&block)