aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb9
-rw-r--r--actionpack/test/dispatch/routing/custom_url_helpers_test.rb14
2 files changed, 23 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 073dabd0a8..cded23ac36 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -2057,6 +2057,15 @@ module ActionDispatch
# [ :products, options.merge(params.permit(:page, :size)) ]
# end
#
+ # In this instance the `params` object comes from the context in which the the
+ # block is executed, e.g. generating a url inside a controller action or a view.
+ # If the block is executed where there isn't a params object such as this:
+ #
+ # Rails.application.routes.url_helpers.browse_path
+ #
+ # then it will raise a `NameError`. Because of this you need to be aware of the
+ # context in which you will use your custom url helper when defining it.
+ #
# NOTE: The `direct` method can't be used inside of a scope block such as
# `namespace` or `scope` and will raise an error if it detects that it is.
def direct(name, options = {}, &block)
diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb
index 6d230a2557..f85b989892 100644
--- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb
+++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb
@@ -96,6 +96,10 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
direct(:options) { |options| [:products, options] }
direct(:defaults, size: 10) { |options| [:products, options] }
+ direct(:browse, page: 1, size: 10) do |options|
+ [:products, options.merge(params.permit(:page, :size).to_h.symbolize_keys)]
+ end
+
resolve("Article") { |article| [:post, { id: article.id }] }
resolve("Basket") { |basket| [:basket] }
resolve("User", anchor: "details") { |user, options| [:profile, options] }
@@ -127,6 +131,10 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
@safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action)
end
+ def params
+ ActionController::Parameters.new(page: 2, size: 25)
+ end
+
def test_direct_paths
assert_equal "http://www.rubyonrails.org", website_path
assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path
@@ -162,6 +170,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
assert_equal "/products?size=10", Routes.url_helpers.defaults_path
assert_equal "/products?size=20", defaults_path(size: 20)
assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20)
+
+ assert_equal "/products?page=2&size=25", browse_path
+ assert_raises(NameError) { Routes.url_helpers.browse_path }
end
def test_direct_urls
@@ -199,6 +210,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url
assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20)
assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20)
+
+ assert_equal "http://www.example.com/products?page=2&size=25", browse_url
+ assert_raises(NameError) { Routes.url_helpers.browse_url }
end
def test_resolve_paths