You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Fixrails#54890
- ### Problem
Calling `polyphormic_url` in a integration test or anywhere else
when routes may not be loaded yet will result in either crashing
or returning a wrong url.
### Details
When the routes are not yet loaded, any `resolve` routes are not
taken in consideration when calling `polymorphic_url`.
```ruby
# config/routes.rb
resolve("Post") { "https://example.org" }
# test.rb
polymorphic_url(@post) # Crash: `post_url` is not defined.
```
### Solution
Load the routes when `polymorphic_mappings` ultimately gets called
by `polymorphic_url`
https://github.com/rails/rails/blob/42a71a03a0adb465dc49b4e21dec8e0ea29e8e35/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb#L179
### Side note
It's worth to note that `polymorphic_url` was overriden to
load the routes, but it only worked if one attempted to call it on
the singleton route's url_helpers:
```ruby
class MyTest << ActionDispatch::IntegrationTest
test "example" do
# Works correctly on main
Rails.application.routes.url_helpers.polymorphic_url(@post)
# Does not work without this patch
polymorphic_url(@post)
end
end
```
I removed the previous implementation because it was at the wrong
level.
0 commit comments