Changeset 7820
- Timestamp:
- 10/09/07 23:07:36 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/base.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/response.rb (modified) (1 diff)
- trunk/actionpack/test/controller/redirect_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r7791 r7820 1 1 *SVN* 2 3 * Add :status to redirect_to allowing users to choose their own response code without manually setting headers. #8297 [codahale, chasgrundy] 2 4 3 5 * 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 988 988 # redirect_to :back 989 989 # 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 991 997 # 992 998 # When using <tt>redirect_to :back</tt>, if there is no referrer, 993 999 # RedirectBackError will be raised. You may specify some fallback 994 1000 # 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 996 1011 case options 997 1012 when %r{^\w+://.*} 998 1013 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)) 1001 1016 response.redirected_to = options 1002 1017 @performed_redirect = true 1003 1018 1004 1019 when String 1005 redirect_to(request.protocol + request.host_with_port + options )1020 redirect_to(request.protocol + request.host_with_port + options, :status=>status) 1006 1021 1007 1022 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) 1009 1024 1010 1025 when Hash 1011 redirect_to(url_for(options) )1026 redirect_to(url_for(options), :status=>status) 1012 1027 response.redirected_to = options 1013 1028 1014 1029 else 1015 redirect_to(url_for(options) )1030 redirect_to(url_for(options), :status=>status) 1016 1031 end 1017 1032 end trunk/actionpack/lib/action_controller/response.rb
r7309 r7820 29 29 end 30 30 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 33 33 self.headers["Location"] = to_url 34 34 trunk/actionpack/test/controller/redirect_test.rb
r6729 r7820 23 23 def simple_redirect 24 24 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 25 53 end 26 54 … … 71 99 assert_response :redirect 72 100 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 73 151 end 74 152