Skip to content

Patch to deal with named_route name collisions... - #3

Closed
rst wants to merge 1 commit into
ianwhite:masterfrom
rst:master
Closed

rst wants to merge 1 commit into
ianwhite:masterfrom
rst:master

Conversation

@rst

@rst rst commented Oct 18, 2010

Copy link
Copy Markdown

Updating my app, I discovered that 'map.resources :requests' breaks recent
versions of resources_controller --- this defines a 'request_path' named route,
which in turn winds up clobbering resources_controller's internal method by
that name.

This is a patch to deal with the issue, by renaming the method to
'path_of_request', which takes it out of the line of fire. Most of it is
updating the specs (which all still pass... at least after a 'mkdir tmp'
so it could create the sqlite3 test db).

FWIW, this is currently lighthouse ticket #9:

http://ianwhite.lighthouseapp.com/projects/10598/tickets/9-request_path-method-gets-clobbered-by-named-routes

Resources_controller defined an internal controller method called
'request_path'.  If you've got a 'map.resources :requests' in your
routes.rb file, that defines a named route called 'request_path'.
When these two meet... wackiness ensues!

To avoid the wackiness (which manifests as complaints from the
code that implements the named route), this patch renames the
'request_path' method, and its companion 'nested_request_path',
so that they're no longer at risk of being trampled by eccentric
named routes.
@jlsync

jlsync commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

Superseded by #19, which is a rebase of this idea onto the current code — thank you for diagnosing this, Robert: you were right, and it has gone unnoticed since 2010.

For the record, the collision is still live on master today (Ruby 4.0.7 / Rails 8.1.4): url_helpers lands at ancestor index 7 and RequestPathIntrospection at 12, so a resources :requests route replaces resources_controller's own request_path. Adding that route to the spec app turns 16 passing examples in spec/lib/request_path_introspection_spec.rb into 6 failures, and 513 failures across the suite, because the invented path is fed to the wildcard loader.

#19 does the same rename you proposed, with the name rc_request_path rather than path_of_request (either is fine — the point is that no plausible route name can collide), updates the 24 spec call sites, and adds a regression spec that pins the lookup order so this cannot regress silently. #nesting_request_path was left alone, since no route is plausibly named that.

One behavioural note carried into the CHANGELOG: request_path was protected and undocumented, but an app overriding it to customise the path will need to move to #rc_request_path (or the documented :resource_path param).

I'll close this as superseded once #19 is merged — the credit for finding it is in the commit message, the PR description and the 5.0.1 CHANGELOG entry.

jlsync added a commit that referenced this pull request Oct 10, 2026
Fix request_path being replaced by a named route helper (supersedes #3)
@jlsync jlsync closed this Oct 10, 2026
jlsync added a commit to jlsync/resources_controller that referenced this pull request Oct 11, 2026
A route like `resources :requests` generates a public `request_path` helper, and
Rails includes the url_helpers module into controllers after
resources_controller's modules (url_helpers sits at ancestor index 7,
RequestPathIntrospection at 12, and the lower index wins).  resources_controller's
own request_path was therefore replaced by the route helper, so it read the
helper's value instead of the path of the current request, and could not work out
the enclosing resources: with a top level `resources :requests` route in the spec
app, 6 examples in spec/lib/request_path_introspection_spec.rb fail.

The internal reader is renamed to rc_request_path, so that no plausible route name
can collide with it, and the specs are updated to match.

Based on PR ianwhite#3 by Robert Thau, which diagnosed this and proposed the same rename
(as path_of_request) but is written against the pre-rename lib/ardes/ layout and
RSpec 2 syntax, so it no longer applies.

Adds a regression spec that includes a module defining a public request_path into
the controller under test - the position url_helpers occupies - and asserts the
path of the request is still read.

Co-authored-by: DeepSeek <agent@deepseek.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants