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

Changeset 7820

Show
Ignore:
Timestamp:
10/09/07 23:07:36 (1 year ago)
Author:
nzkoz
Message:

Add :status to redirect_to allowing users to choose their own response code without manually setting headers. Closes #8297 [codahale, chasgrundy]

Files:

Legend:

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

    r7791 r7820  
    11*SVN* 
     2 
     3* Add :status to redirect_to allowing users to choose their own response code without manually setting headers. #8297 [codahale, chasgrundy] 
    24 
    35* Add link_to :back which uses your referrer with a fallback to a javascript link. #7366 [eventualbuddha, tarmo] 
  • trunk/actionpack/lib/action_controller/base.rb

    r7757 r7820  
    988988      #   redirect_to :back 
    989989      # 
    990       # The redirection happens as a "302 Moved" header. 
     990      # The redirection happens as a "302 Moved" header unless otherwise specified.  
     991      # 
     992      # Examples: 
     993      #   redirect_to post_url(@post), :status=>:found 
     994      #   redirect_to :action=>'atom', :status=>:moved_permanently 
     995      #   redirect_to post_url(@post), :status=>301 
     996      #   redirect_to :action=>'atom', :status=>302 
    991997      # 
    992998      # When using <tt>redirect_to :back</tt>, if there is no referrer, 
    993999      # RedirectBackError will be raised. You may specify some fallback 
    9941000      # behavior for this case by rescuing RedirectBackError. 
    995       def redirect_to(options = {}) #:doc: 
     1001      def redirect_to(options = {}, response_status = {}) #:doc:  
     1002         
     1003        if options.is_a?(Hash) && options[:status]  
     1004          status = options.delete(:status)  
     1005        elsif response_status[:status]  
     1006          status = response_status[:status]  
     1007        else  
     1008          status = 302  
     1009        end 
     1010         
    9961011        case options 
    9971012          when %r{^\w+://.*} 
    9981013            raise DoubleRenderError if performed? 
    999             logger.info("Redirected to #{options}") if logger 
    1000             response.redirect(options
     1014            logger.info("Redirected to #{options}") if logger && logger.info? 
     1015            response.redirect(options, interpret_status(status)
    10011016            response.redirected_to = options 
    10021017            @performed_redirect = true 
    10031018 
    10041019          when String 
    1005             redirect_to(request.protocol + request.host_with_port + options
     1020            redirect_to(request.protocol + request.host_with_port + options, :status=>status
    10061021 
    10071022          when :back 
    1008             request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : raise(RedirectBackError) 
     1023            request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"], :status=>status) : raise(RedirectBackError) 
    10091024 
    10101025          when Hash 
    1011             redirect_to(url_for(options)
     1026            redirect_to(url_for(options), :status=>status
    10121027            response.redirected_to = options 
    10131028 
    10141029          else 
    1015             redirect_to(url_for(options)
     1030            redirect_to(url_for(options), :status=>status
    10161031        end 
    10171032      end 
  • trunk/actionpack/lib/action_controller/response.rb

    r7309 r7820  
    2929    end 
    3030 
    31     def redirect(to_url, permanently = false
    32       self.headers["Status"]   = "302 Found" unless headers["Status"] == "301 Moved Permanently" 
     31    def redirect(to_url, response_status
     32      self.headers["Status"] = response_status 
    3333      self.headers["Location"] = to_url 
    3434 
  • trunk/actionpack/test/controller/redirect_test.rb

    r6729 r7820  
    2323  def simple_redirect 
    2424    redirect_to :action => "hello_world" 
     25  end 
     26 
     27  def redirect_with_status  
     28    redirect_to({:action => "hello_world", :status => 301}) 
     29  end  
     30 
     31  def redirect_with_status_hash 
     32    redirect_to({:action => "hello_world"}, {:status => 301}) 
     33  end  
     34 
     35  def url_redirect_with_status  
     36    redirect_to("http://www.example.com", :status => :moved_permanently) 
     37  end  
     38    
     39  def url_redirect_with_status_hash  
     40    redirect_to("http://www.example.com", {:status => 301}) 
     41  end  
     42 
     43  def relative_url_redirect_with_status  
     44    redirect_to("/things/stuff", :status => :found) 
     45  end  
     46    
     47  def relative_url_redirect_with_status_hash 
     48    redirect_to("/things/stuff", {:status => 301}) 
     49  end  
     50    
     51  def redirect_to_back_with_status  
     52    redirect_to :back, :status => 307  
    2553  end 
    2654 
     
    7199    assert_response :redirect 
    72100    assert_equal "http://test.host/redirect/hello_world", redirect_to_url 
     101  end 
     102 
     103  def test_redirect_with_no_status 
     104    get :simple_redirect 
     105    assert_response 302 
     106    assert_equal "http://test.host/redirect/hello_world", redirect_to_url 
     107  end 
     108 
     109  def test_redirect_with_status  
     110    get :redirect_with_status  
     111    assert_response 301  
     112    assert_equal "http://test.host/redirect/hello_world", redirect_to_url  
     113  end  
     114 
     115  def test_redirect_with_status_hash  
     116    get :redirect_with_status_hash 
     117    assert_response 301  
     118    assert_equal "http://test.host/redirect/hello_world", redirect_to_url  
     119  end 
     120    
     121  def test_url_redirect_with_status  
     122    get :url_redirect_with_status  
     123    assert_response 301  
     124    assert_equal "http://www.example.com", redirect_to_url  
     125  end  
     126 
     127  def test_url_redirect_with_status_hash 
     128    get :url_redirect_with_status_hash 
     129    assert_response 301  
     130    assert_equal "http://www.example.com", redirect_to_url  
     131  end  
     132 
     133   
     134  def test_relative_url_redirect_with_status  
     135    get :relative_url_redirect_with_status  
     136    assert_response 302 
     137    assert_equal "http://test.host/things/stuff", redirect_to_url  
     138  end  
     139    
     140  def test_relative_url_redirect_with_status_hash 
     141    get :relative_url_redirect_with_status_hash 
     142    assert_response 301  
     143    assert_equal "http://test.host/things/stuff", redirect_to_url  
     144  end    
     145    
     146  def test_redirect_to_back_with_status  
     147    @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"  
     148    get :redirect_to_back_with_status  
     149    assert_response 307  
     150    assert_equal "http://www.example.com/coming/from", redirect_to_url  
    73151  end 
    74152