New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom ArgumentError for invalid to: values #50466
Add custom ArgumentError for invalid to: values #50466
Conversation
A lot of objects return a User.new.to_s
=> "#<User:0x00000001285ec1c0>" So I think raising an |
The more I think about it the more I agree that we should probably be raising. I suppose the question is whether a change like that would be backportable since its a larger change in behavior. I'm kind of thinking the ideal flow should be:
Supporting Symbols or Strings that don't include a |
We should probably still support the following (mentioned in the issue), as it's documented in the API docs: controller :blog do
get 'blog/show', to: :list
get 'blog/delete', to: :delete
get 'blog/edit', to: :edit
end I'd expect we'd have a failing test for that. |
Hmm, not sure if any of this ever worked as documented: rails/actionpack/lib/action_dispatch/routing.rb Lines 120 to 124 in 4d50600
rails/actionpack/lib/action_dispatch/routing.rb Lines 106 to 110 in 396bb77
|
That's what's interesting, those examples do not work even with the current patch due to
These I found actually are tested: rails/actionpack/test/dispatch/routing_test.rb Lines 49 to 52 in e3da4fc
it's just |
It seems the following commit accidentally broke some examples. “to:” was added where it didn’t belong: fc54809 |
Yes, let's raise a better exception. |
b39934a
to
a397764
Compare
👍 Updated with a new |
Previously, it was theoretically possible to define a route with a Symbol as a `to:` value (or at least, it would not raise a `NoMethodError`). However, passing a Symbol broke when `/#/.match?(to)` was [replaced][1] with `to&.include?("#")` with the assumption that `to` was always a String. Instead of restoring the previous error, this commit improves how the `to:` value is checked so that it raises an `ArgumentError` for any invalid values. The extra strictness will specifically improve the error when a Symbol or String that doesn't include a "#" are passed since they were effectively equivalent to passing a `nil` value, or not specifying `to:` at all. [1]: 5726b1d
6ffae37
to
a39332f
Compare
…methoderror Add custom ArgumentError for invalid to: values
Motivation / Background
Ref #50465
Previously, it was theoretically possible to define a route with a Symbol as a
to:
value (or at least, it would not raise aNoMethodError
). However, passing a Symbol broke when/#/.match?(to)
was replaced withto&.include?("#")
with the assumption thatto
was always a String.Detail
This commit fixes the issue by callingto_s
on theto:
value before callingincludes?
to prevent theNoMethodError
.Instead of restoring the previous error, this commit improves how the
to:
value is checked so that it raises anArgumentError
for any invalid values. The extra strictness will specifically improve the error when a Symbol or String that doesn't include a "#" are passed since they were effectively equivalent to passing anil
value, or not specifyingto:
at all.Additional information
I considered writing a test but testing that a symbol is allowed seems wrong when it doesn't actually work (it tries to split the symbol into controller/action, and returns nil for both if symbol). Maybe alternatively we proactively raise anArgumentError
if theto
value isn't aString
(after therespond_to?
checks)?If accepted, this should be backported toIs this still backport worthy?7-1-stable
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]