actionpack 7.0.8.7 → 7.2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +90 -537
- data/MIT-LICENSE +1 -1
- data/README.rdoc +2 -2
- data/lib/abstract_controller/asset_paths.rb +2 -0
- data/lib/abstract_controller/base.rb +119 -106
- data/lib/abstract_controller/caching/fragments.rb +51 -52
- data/lib/abstract_controller/caching.rb +2 -0
- data/lib/abstract_controller/callbacks.rb +94 -67
- data/lib/abstract_controller/collector.rb +6 -6
- data/lib/abstract_controller/deprecator.rb +9 -0
- data/lib/abstract_controller/error.rb +2 -0
- data/lib/abstract_controller/helpers.rb +121 -91
- data/lib/abstract_controller/logger.rb +2 -0
- data/lib/abstract_controller/railties/routes_helpers.rb +3 -16
- data/lib/abstract_controller/rendering.rb +14 -13
- data/lib/abstract_controller/translation.rb +12 -30
- data/lib/abstract_controller/url_for.rb +9 -5
- data/lib/abstract_controller.rb +8 -0
- data/lib/action_controller/api/api_rendering.rb +2 -0
- data/lib/action_controller/api.rb +78 -73
- data/lib/action_controller/base.rb +199 -141
- data/lib/action_controller/caching.rb +16 -11
- data/lib/action_controller/deprecator.rb +9 -0
- data/lib/action_controller/form_builder.rb +21 -16
- data/lib/action_controller/log_subscriber.rb +19 -5
- data/lib/action_controller/metal/allow_browser.rb +123 -0
- data/lib/action_controller/metal/basic_implicit_render.rb +2 -0
- data/lib/action_controller/metal/conditional_get.rb +187 -174
- data/lib/action_controller/metal/content_security_policy.rb +26 -25
- data/lib/action_controller/metal/cookies.rb +4 -2
- data/lib/action_controller/metal/data_streaming.rb +65 -54
- data/lib/action_controller/metal/default_headers.rb +6 -2
- data/lib/action_controller/metal/etag_with_flash.rb +4 -0
- data/lib/action_controller/metal/etag_with_template_digest.rb +18 -14
- data/lib/action_controller/metal/exceptions.rb +19 -9
- data/lib/action_controller/metal/flash.rb +12 -10
- data/lib/action_controller/metal/head.rb +20 -16
- data/lib/action_controller/metal/helpers.rb +64 -67
- data/lib/action_controller/metal/http_authentication.rb +212 -199
- data/lib/action_controller/metal/implicit_render.rb +21 -17
- data/lib/action_controller/metal/instrumentation.rb +22 -12
- data/lib/action_controller/metal/live.rb +125 -92
- data/lib/action_controller/metal/logging.rb +6 -4
- data/lib/action_controller/metal/mime_responds.rb +151 -142
- data/lib/action_controller/metal/parameter_encoding.rb +34 -32
- data/lib/action_controller/metal/params_wrapper.rb +58 -58
- data/lib/action_controller/metal/permissions_policy.rb +14 -13
- data/lib/action_controller/metal/rate_limiting.rb +62 -0
- data/lib/action_controller/metal/redirecting.rb +110 -84
- data/lib/action_controller/metal/renderers.rb +50 -49
- data/lib/action_controller/metal/rendering.rb +103 -82
- data/lib/action_controller/metal/request_forgery_protection.rb +279 -161
- data/lib/action_controller/metal/rescue.rb +12 -8
- data/lib/action_controller/metal/streaming.rb +174 -132
- data/lib/action_controller/metal/strong_parameters.rb +598 -473
- data/lib/action_controller/metal/testing.rb +2 -0
- data/lib/action_controller/metal/url_for.rb +23 -14
- data/lib/action_controller/metal.rb +145 -61
- data/lib/action_controller/railtie.rb +25 -9
- data/lib/action_controller/railties/helpers.rb +2 -0
- data/lib/action_controller/renderer.rb +105 -66
- data/lib/action_controller/template_assertions.rb +4 -2
- data/lib/action_controller/test_case.rb +157 -128
- data/lib/action_controller.rb +17 -3
- data/lib/action_dispatch/constants.rb +34 -0
- data/lib/action_dispatch/deprecator.rb +9 -0
- data/lib/action_dispatch/http/cache.rb +28 -29
- data/lib/action_dispatch/http/content_disposition.rb +2 -0
- data/lib/action_dispatch/http/content_security_policy.rb +48 -45
- data/lib/action_dispatch/http/filter_parameters.rb +18 -8
- data/lib/action_dispatch/http/filter_redirect.rb +22 -1
- data/lib/action_dispatch/http/headers.rb +23 -21
- data/lib/action_dispatch/http/mime_negotiation.rb +37 -48
- data/lib/action_dispatch/http/mime_type.rb +60 -30
- data/lib/action_dispatch/http/mime_types.rb +5 -1
- data/lib/action_dispatch/http/parameters.rb +12 -10
- data/lib/action_dispatch/http/permissions_policy.rb +32 -27
- data/lib/action_dispatch/http/rack_cache.rb +4 -0
- data/lib/action_dispatch/http/request.rb +132 -79
- data/lib/action_dispatch/http/response.rb +136 -103
- data/lib/action_dispatch/http/upload.rb +19 -15
- data/lib/action_dispatch/http/url.rb +75 -73
- data/lib/action_dispatch/journey/formatter.rb +19 -6
- data/lib/action_dispatch/journey/gtg/builder.rb +4 -3
- data/lib/action_dispatch/journey/gtg/simulator.rb +2 -0
- data/lib/action_dispatch/journey/gtg/transition_table.rb +10 -8
- data/lib/action_dispatch/journey/nfa/dot.rb +2 -0
- data/lib/action_dispatch/journey/nodes/node.rb +6 -5
- data/lib/action_dispatch/journey/parser.rb +4 -3
- data/lib/action_dispatch/journey/parser_extras.rb +2 -0
- data/lib/action_dispatch/journey/path/pattern.rb +18 -15
- data/lib/action_dispatch/journey/route.rb +12 -9
- data/lib/action_dispatch/journey/router/utils.rb +16 -15
- data/lib/action_dispatch/journey/router.rb +13 -10
- data/lib/action_dispatch/journey/routes.rb +6 -4
- data/lib/action_dispatch/journey/scanner.rb +4 -2
- data/lib/action_dispatch/journey/visitors.rb +2 -0
- data/lib/action_dispatch/journey.rb +2 -0
- data/lib/action_dispatch/log_subscriber.rb +25 -0
- data/lib/action_dispatch/middleware/actionable_exceptions.rb +7 -6
- data/lib/action_dispatch/middleware/assume_ssl.rb +27 -0
- data/lib/action_dispatch/middleware/callbacks.rb +4 -0
- data/lib/action_dispatch/middleware/cookies.rb +192 -194
- data/lib/action_dispatch/middleware/debug_exceptions.rb +36 -27
- data/lib/action_dispatch/middleware/debug_locks.rb +18 -13
- data/lib/action_dispatch/middleware/debug_view.rb +9 -2
- data/lib/action_dispatch/middleware/exception_wrapper.rb +181 -27
- data/lib/action_dispatch/middleware/executor.rb +9 -1
- data/lib/action_dispatch/middleware/flash.rb +65 -46
- data/lib/action_dispatch/middleware/host_authorization.rb +22 -17
- data/lib/action_dispatch/middleware/public_exceptions.rb +12 -8
- data/lib/action_dispatch/middleware/reloader.rb +9 -5
- data/lib/action_dispatch/middleware/remote_ip.rb +88 -83
- data/lib/action_dispatch/middleware/request_id.rb +15 -8
- data/lib/action_dispatch/middleware/server_timing.rb +8 -6
- data/lib/action_dispatch/middleware/session/abstract_store.rb +7 -0
- data/lib/action_dispatch/middleware/session/cache_store.rb +14 -7
- data/lib/action_dispatch/middleware/session/cookie_store.rb +32 -25
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +9 -3
- data/lib/action_dispatch/middleware/show_exceptions.rb +42 -28
- data/lib/action_dispatch/middleware/ssl.rb +60 -45
- data/lib/action_dispatch/middleware/stack.rb +15 -9
- data/lib/action_dispatch/middleware/static.rb +40 -34
- data/lib/action_dispatch/middleware/templates/rescues/_actions.html.erb +2 -2
- data/lib/action_dispatch/middleware/templates/rescues/_message_and_suggestions.html.erb +4 -4
- data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +8 -1
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +7 -7
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +2 -2
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +17 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +16 -12
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +3 -3
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +4 -4
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +1 -1
- data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +3 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +47 -38
- data/lib/action_dispatch/railtie.rb +12 -4
- data/lib/action_dispatch/request/session.rb +39 -27
- data/lib/action_dispatch/request/utils.rb +10 -3
- data/lib/action_dispatch/routing/endpoint.rb +2 -0
- data/lib/action_dispatch/routing/inspector.rb +59 -9
- data/lib/action_dispatch/routing/mapper.rb +686 -639
- data/lib/action_dispatch/routing/polymorphic_routes.rb +70 -61
- data/lib/action_dispatch/routing/redirection.rb +52 -38
- data/lib/action_dispatch/routing/route_set.rb +106 -62
- data/lib/action_dispatch/routing/routes_proxy.rb +16 -19
- data/lib/action_dispatch/routing/url_for.rb +131 -122
- data/lib/action_dispatch/routing.rb +152 -150
- data/lib/action_dispatch/system_test_case.rb +91 -81
- data/lib/action_dispatch/system_testing/browser.rb +27 -19
- data/lib/action_dispatch/system_testing/driver.rb +16 -22
- data/lib/action_dispatch/system_testing/server.rb +2 -0
- data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +53 -31
- data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +2 -0
- data/lib/action_dispatch/testing/assertion_response.rb +9 -7
- data/lib/action_dispatch/testing/assertions/response.rb +36 -26
- data/lib/action_dispatch/testing/assertions/routing.rb +203 -95
- data/lib/action_dispatch/testing/assertions.rb +5 -1
- data/lib/action_dispatch/testing/integration.rb +240 -229
- data/lib/action_dispatch/testing/request_encoder.rb +6 -1
- data/lib/action_dispatch/testing/test_helpers/page_dump_helper.rb +35 -0
- data/lib/action_dispatch/testing/test_process.rb +14 -9
- data/lib/action_dispatch/testing/test_request.rb +4 -2
- data/lib/action_dispatch/testing/test_response.rb +34 -19
- data/lib/action_dispatch.rb +52 -21
- data/lib/action_pack/gem_version.rb +6 -4
- data/lib/action_pack/version.rb +3 -1
- data/lib/action_pack.rb +18 -17
- metadata +86 -27
@@ -1,102 +1,111 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# :markup: markdown
|
4
|
+
|
3
5
|
module ActionDispatch
|
4
6
|
module Routing
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
7
|
+
# # Action Dispatch Routing PolymorphicRoutes
|
8
|
+
#
|
9
|
+
# Polymorphic URL helpers are methods for smart resolution to a named route call
|
10
|
+
# when given an Active Record model instance. They are to be used in combination
|
11
|
+
# with ActionController::Resources.
|
8
12
|
#
|
9
|
-
# These methods are useful when you want to generate the correct URL or path to
|
10
|
-
# resource without having to know the exact type of the record in
|
13
|
+
# These methods are useful when you want to generate the correct URL or path to
|
14
|
+
# a RESTful resource without having to know the exact type of the record in
|
15
|
+
# question.
|
11
16
|
#
|
12
|
-
# Nested resources and/or namespaces are also supported, as illustrated in the
|
17
|
+
# Nested resources and/or namespaces are also supported, as illustrated in the
|
18
|
+
# example:
|
13
19
|
#
|
14
|
-
#
|
20
|
+
# polymorphic_url([:admin, @article, @comment])
|
15
21
|
#
|
16
22
|
# results in:
|
17
23
|
#
|
18
|
-
#
|
24
|
+
# admin_article_comment_url(@article, @comment)
|
25
|
+
#
|
26
|
+
# ## Usage within the framework
|
19
27
|
#
|
20
|
-
#
|
28
|
+
# Polymorphic URL helpers are used in a number of places throughout the Rails
|
29
|
+
# framework:
|
21
30
|
#
|
22
|
-
#
|
31
|
+
# * `url_for`, so you can use it with a record as the argument, e.g.
|
32
|
+
# `url_for(@article)`;
|
33
|
+
# * ActionView::Helpers::FormHelper uses `polymorphic_path`, so you can write
|
34
|
+
# `form_for(@article)` without having to specify `:url` parameter for the
|
35
|
+
# form action;
|
36
|
+
# * `redirect_to` (which, in fact, uses `url_for`) so you can write
|
37
|
+
# `redirect_to(post)` in your controllers;
|
38
|
+
# * ActionView::Helpers::AtomFeedHelper, so you don't have to explicitly
|
39
|
+
# specify URLs for feed entries.
|
23
40
|
#
|
24
|
-
# * <tt>url_for</tt>, so you can use it with a record as the argument, e.g.
|
25
|
-
# <tt>url_for(@article)</tt>;
|
26
|
-
# * ActionView::Helpers::FormHelper uses <tt>polymorphic_path</tt>, so you can write
|
27
|
-
# <tt>form_for(@article)</tt> without having to specify <tt>:url</tt> parameter for the form
|
28
|
-
# action;
|
29
|
-
# * <tt>redirect_to</tt> (which, in fact, uses <tt>url_for</tt>) so you can write
|
30
|
-
# <tt>redirect_to(post)</tt> in your controllers;
|
31
|
-
# * ActionView::Helpers::AtomFeedHelper, so you don't have to explicitly specify URLs
|
32
|
-
# for feed entries.
|
33
41
|
#
|
34
|
-
#
|
42
|
+
# ## Prefixed polymorphic helpers
|
35
43
|
#
|
36
|
-
# In addition to
|
37
|
-
#
|
38
|
-
#
|
44
|
+
# In addition to `polymorphic_url` and `polymorphic_path` methods, a number of
|
45
|
+
# prefixed helpers are available as a shorthand to `action: "..."` in options.
|
46
|
+
# Those are:
|
47
|
+
#
|
48
|
+
# * `edit_polymorphic_url`, `edit_polymorphic_path`
|
49
|
+
# * `new_polymorphic_url`, `new_polymorphic_path`
|
39
50
|
#
|
40
|
-
# * <tt>edit_polymorphic_url</tt>, <tt>edit_polymorphic_path</tt>
|
41
|
-
# * <tt>new_polymorphic_url</tt>, <tt>new_polymorphic_path</tt>
|
42
51
|
#
|
43
52
|
# Example usage:
|
44
53
|
#
|
45
|
-
#
|
46
|
-
#
|
54
|
+
# edit_polymorphic_path(@post) # => "/posts/1/edit"
|
55
|
+
# polymorphic_path(@post, format: :pdf) # => "/posts/1.pdf"
|
47
56
|
#
|
48
|
-
#
|
57
|
+
# ## Usage with mounted engines
|
49
58
|
#
|
50
59
|
# If you are using a mounted engine and you need to use a polymorphic_url
|
51
60
|
# pointing at the engine's routes, pass in the engine's route proxy as the first
|
52
61
|
# argument to the method. For example:
|
53
62
|
#
|
54
|
-
#
|
55
|
-
#
|
63
|
+
# polymorphic_url([blog, @post]) # calls blog.post_path(@post)
|
64
|
+
# form_for([blog, @post]) # => "/blog/posts/1"
|
56
65
|
#
|
57
66
|
module PolymorphicRoutes
|
58
|
-
# Constructs a call to a named RESTful route for the given record and returns
|
59
|
-
# resulting URL string. For example:
|
67
|
+
# Constructs a call to a named RESTful route for the given record and returns
|
68
|
+
# the resulting URL string. For example:
|
69
|
+
#
|
70
|
+
# # calls post_url(post)
|
71
|
+
# polymorphic_url(post) # => "http://example.com/posts/1"
|
72
|
+
# polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
|
73
|
+
# polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
|
74
|
+
# polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
|
75
|
+
# polymorphic_url(Comment) # => "http://example.com/comments"
|
60
76
|
#
|
61
|
-
#
|
62
|
-
# polymorphic_url(post) # => "http://example.com/posts/1"
|
63
|
-
# polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
|
64
|
-
# polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
|
65
|
-
# polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
|
66
|
-
# polymorphic_url(Comment) # => "http://example.com/comments"
|
77
|
+
# #### Options
|
67
78
|
#
|
68
|
-
#
|
79
|
+
# * `:action` - Specifies the action prefix for the named route: `:new` or
|
80
|
+
# `:edit`. Default is no prefix.
|
81
|
+
# * `:routing_type` - Allowed values are `:path` or `:url`. Default is `:url`.
|
69
82
|
#
|
70
|
-
# * <tt>:action</tt> - Specifies the action prefix for the named route:
|
71
|
-
# <tt>:new</tt> or <tt>:edit</tt>. Default is no prefix.
|
72
|
-
# * <tt>:routing_type</tt> - Allowed values are <tt>:path</tt> or <tt>:url</tt>.
|
73
|
-
# Default is <tt>:url</tt>.
|
74
83
|
#
|
75
|
-
# Also includes all the options from
|
76
|
-
#
|
77
|
-
# is given below:
|
84
|
+
# Also includes all the options from `url_for`. These include such things as
|
85
|
+
# `:anchor` or `:trailing_slash`. Example usage is given below:
|
78
86
|
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
87
|
+
# polymorphic_url([blog, post], anchor: 'my_anchor')
|
88
|
+
# # => "http://example.com/blogs/1/posts/1#my_anchor"
|
89
|
+
# polymorphic_url([blog, post], anchor: 'my_anchor', script_name: "/my_app")
|
90
|
+
# # => "http://example.com/my_app/blogs/1/posts/1#my_anchor"
|
83
91
|
#
|
84
|
-
# For all of these options, see the documentation for
|
92
|
+
# For all of these options, see the documentation for
|
93
|
+
# [url_for](rdoc-ref:ActionDispatch::Routing::UrlFor).
|
85
94
|
#
|
86
|
-
#
|
95
|
+
# #### Functionality
|
87
96
|
#
|
88
|
-
#
|
89
|
-
#
|
97
|
+
# # an Article record
|
98
|
+
# polymorphic_url(record) # same as article_url(record)
|
90
99
|
#
|
91
|
-
#
|
92
|
-
#
|
100
|
+
# # a Comment record
|
101
|
+
# polymorphic_url(record) # same as comment_url(record)
|
93
102
|
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
103
|
+
# # it recognizes new records and maps to the collection
|
104
|
+
# record = Comment.new
|
105
|
+
# polymorphic_url(record) # same as comments_url()
|
97
106
|
#
|
98
|
-
#
|
99
|
-
#
|
107
|
+
# # the class of a record will also map to the collection
|
108
|
+
# polymorphic_url(Comment) # same as comments_url()
|
100
109
|
#
|
101
110
|
def polymorphic_url(record_or_hash_or_array, options = {})
|
102
111
|
if Hash === record_or_hash_or_array
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# :markup: markdown
|
4
|
+
|
3
5
|
require "active_support/core_ext/array/extract_options"
|
4
6
|
require "rack/utils"
|
5
7
|
require "action_controller/metal/exceptions"
|
@@ -18,10 +20,19 @@ module ActionDispatch
|
|
18
20
|
def redirect?; true; end
|
19
21
|
|
20
22
|
def call(env)
|
21
|
-
|
23
|
+
ActiveSupport::Notifications.instrument("redirect.action_dispatch") do |payload|
|
24
|
+
request = Request.new(env)
|
25
|
+
response = build_response(request)
|
26
|
+
|
27
|
+
payload[:status] = @status
|
28
|
+
payload[:location] = response.headers["Location"]
|
29
|
+
payload[:request] = request
|
30
|
+
|
31
|
+
response.to_a
|
32
|
+
end
|
22
33
|
end
|
23
34
|
|
24
|
-
def
|
35
|
+
def build_response(req)
|
25
36
|
uri = URI.parse(path(req.path_parameters, req))
|
26
37
|
|
27
38
|
unless uri.host
|
@@ -38,15 +49,15 @@ module ActionDispatch
|
|
38
49
|
|
39
50
|
req.commit_flash
|
40
51
|
|
41
|
-
body =
|
52
|
+
body = ""
|
42
53
|
|
43
54
|
headers = {
|
44
55
|
"Location" => uri.to_s,
|
45
|
-
"Content-Type" => "text/html",
|
56
|
+
"Content-Type" => "text/html; charset=#{ActionDispatch::Response.default_charset}",
|
46
57
|
"Content-Length" => body.length.to_s
|
47
58
|
}
|
48
59
|
|
49
|
-
|
60
|
+
ActionDispatch::Response.new(status, headers, body)
|
50
61
|
end
|
51
62
|
|
52
63
|
def path(params, request)
|
@@ -59,7 +70,7 @@ module ActionDispatch
|
|
59
70
|
|
60
71
|
private
|
61
72
|
def relative_path?(path)
|
62
|
-
path && !path.empty? && path
|
73
|
+
path && !path.empty? && !path.start_with?("/")
|
63
74
|
end
|
64
75
|
|
65
76
|
def escape(params)
|
@@ -137,55 +148,58 @@ module ActionDispatch
|
|
137
148
|
module Redirection
|
138
149
|
# Redirect any path to another path:
|
139
150
|
#
|
140
|
-
#
|
151
|
+
# get "/stories" => redirect("/posts")
|
141
152
|
#
|
142
|
-
# This will redirect the user, while ignoring certain parts of the request,
|
143
|
-
#
|
153
|
+
# This will redirect the user, while ignoring certain parts of the request,
|
154
|
+
# including query string, etc. `/stories`, `/stories?foo=bar`, etc all redirect
|
155
|
+
# to `/posts`.
|
144
156
|
#
|
145
|
-
# The redirect will use a
|
146
|
-
#
|
157
|
+
# The redirect will use a `301 Moved Permanently` status code by default. This
|
158
|
+
# can be overridden with the `:status` option:
|
147
159
|
#
|
148
|
-
#
|
160
|
+
# get "/stories" => redirect("/posts", status: 307)
|
149
161
|
#
|
150
162
|
# You can also use interpolation in the supplied redirect argument:
|
151
163
|
#
|
152
|
-
#
|
164
|
+
# get 'docs/:article', to: redirect('/wiki/%{article}')
|
153
165
|
#
|
154
|
-
# Note that if you return a path without a leading slash then the URL is
|
155
|
-
# current SCRIPT_NAME environment variable. This is typically
|
156
|
-
# a mounted engine or where the application is
|
166
|
+
# Note that if you return a path without a leading slash then the URL is
|
167
|
+
# prefixed with the current SCRIPT_NAME environment variable. This is typically
|
168
|
+
# '/' but may be different in a mounted engine or where the application is
|
169
|
+
# deployed to a subdirectory of a website.
|
157
170
|
#
|
158
171
|
# Alternatively you can use one of the other syntaxes:
|
159
172
|
#
|
160
|
-
# The block version of redirect allows for the easy encapsulation of any logic
|
161
|
-
# the redirect in question. Either the params and request are
|
162
|
-
# params, depending of how many arguments your
|
163
|
-
# return value.
|
173
|
+
# The block version of redirect allows for the easy encapsulation of any logic
|
174
|
+
# associated with the redirect in question. Either the params and request are
|
175
|
+
# supplied as arguments, or just params, depending of how many arguments your
|
176
|
+
# block accepts. A string is required as a return value.
|
164
177
|
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
#
|
178
|
+
# get 'jokes/:number', to: redirect { |params, request|
|
179
|
+
# path = (params[:number].to_i.even? ? "wheres-the-beef" : "i-love-lamp")
|
180
|
+
# "http://#{request.host_with_port}/#{path}"
|
181
|
+
# }
|
169
182
|
#
|
170
|
-
# Note that the
|
171
|
-
# the block to
|
183
|
+
# Note that the `do end` syntax for the redirect block wouldn't work, as Ruby
|
184
|
+
# would pass the block to `get` instead of `redirect`. Use `{ ... }` instead.
|
172
185
|
#
|
173
|
-
# The options version of redirect allows you to supply only the parts of the URL
|
174
|
-
# to change, it also supports interpolation of the path similar to
|
186
|
+
# The options version of redirect allows you to supply only the parts of the URL
|
187
|
+
# which need to change, it also supports interpolation of the path similar to
|
188
|
+
# the first example.
|
175
189
|
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
190
|
+
# get 'stores/:name', to: redirect(subdomain: 'stores', path: '/%{name}')
|
191
|
+
# get 'stores/:name(*all)', to: redirect(subdomain: 'stores', path: '/%{name}%{all}')
|
192
|
+
# get '/stories', to: redirect(path: '/posts')
|
179
193
|
#
|
180
|
-
# This will redirect the user, while changing only the specified parts of the
|
181
|
-
# for example the
|
182
|
-
#
|
194
|
+
# This will redirect the user, while changing only the specified parts of the
|
195
|
+
# request, for example the `path` option in the last example. `/stories`,
|
196
|
+
# `/stories?foo=bar`, redirect to `/posts` and `/posts?foo=bar` respectively.
|
183
197
|
#
|
184
|
-
# Finally, an object which responds to call can be supplied to redirect,
|
185
|
-
# common redirect routes. The call method must accept two
|
186
|
-
# a string.
|
198
|
+
# Finally, an object which responds to call can be supplied to redirect,
|
199
|
+
# allowing you to reuse common redirect routes. The call method must accept two
|
200
|
+
# arguments, params and request, and return a string.
|
187
201
|
#
|
188
|
-
#
|
202
|
+
# get 'accounts/:name' => redirect(SubdomainRedirector.new('api'))
|
189
203
|
#
|
190
204
|
def redirect(*args, &block)
|
191
205
|
options = args.extract_options!
|