-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
Let link_to infer link name from Model#to_s #42234
Let link_to infer link name from Model#to_s #42234
Conversation
| @@ -146,6 +148,12 @@ def _filtered_referrer # :nodoc: | |||
| # link_to nil, "http://example.com" | |||
| # # => <a href="http://www.example.com">http://www.example.com</a> | |||
| # | |||
| # Pithier yet, when name is an ActiveRecord model that defines a | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand the use of "pithier" here but perhaps my English skills are deteriorating 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pithy means concise or forcefully expressive. Something concise and full of meaning, which IMO is exactly what a model is in relation to a URL helper. 🤗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said we can change it to "concise" to be more ESL friendly. I'll just point out I'm the ESL learner and you're the native English speaker. 😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind you I think I just used the same adjective I found earlier in the docs:
| # or the even pithier |
d09953f to
3f90726
Compare
| @@ -146,6 +148,12 @@ def _filtered_referrer # :nodoc: | |||
| # link_to nil, "http://example.com" | |||
| # # => <a href="http://www.example.com">http://www.example.com</a> | |||
| # | |||
| # More concise yet, when +name+ is an ActiveRecord model that defines a | |||
| # +to_s+ method returning a default value or a model instance attribute | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zzak I changed this from:
+to_s+ method returning a name attribute or a default value
To this:
+to_s+ method returning a default value or a model instance attribute
I feel like the mention of a "name attribute" could have been misunderstood as the +name+ argument to link_to when really I meant a completely different model instance attribute called name. That was too confusing, the implementation of whatever custom #to_s is irrelevant.
|
In principle I'm 👍🏻 on the feature, but not keen on overloading |
|
@pixeltrix Good point, I'll change the docs to reference |
|
Actually @pixeltrix, I don't think I agree. I read your response too fast.
In my experience with Rails apps, most often there is no implementation of We could recommend alternatives but there's nothing in my implementation that calls |
I don't think the problem is with In this case, interpolation has a dependency on the Maybe we could add a |
|
I think
|
Ah, that's a good point and does seem very natural. I'm sold on |
I guess that's a consensus then 🙃 My other concern is that what's the out-of-the-box experience? Because currently they'll just get |
|
@pixeltrix to me the argument still holds: that's what you'd already get from I don't really see this as providing new deep link-label-guessing magic, and more just about making |
Speaking of good points. 😃 Maybe exposing a default It's not perfect for printing the identity of non-AR-models or models with no primary keys (or one not called It's almost like scaffolds if you think about it. The default behavior isn't great but you can easily improve it by bringing your own implementation. |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
|
Could you add a CHANGELOG entry? |
|
@ghiculescu Sure thing. |
9df31a9 to
dbe7099
Compare
|
@ghiculescu Pending CI but I think we're good to merge after this. Added the CHANGELOG entry at the top. Let me know if you think the entry is helpful: |
|
I doubt the failure in actionmailer(master) is related to this PR: https://buildkite.com/rails/rails/builds/80300#634c5085-65ce-4310-98b3-44a3ba1f6e2d/1086-1104 Ditto for railties(master): https://buildkite.com/rails/rails/builds/80300#87580cb6-7e4c-4231-ab54-d4fc0994a4b2/1330-1332 |
|
Maybe the changelog should show how to_s is implemented on the model, to complete the picture. |
dbe7099 to
1e9629e
Compare
Yeah I think that's helpful. Updated to: |
|
Thanks @rafaelfranca 😊 |
Fixes rails#43913 which found a regression in rails#42234
| if name.respond_to?(:model_name) && options.empty? | ||
| url_for(name) | ||
| else | ||
| url_for(options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny story: an app that is just getting upgraded to Rails 7 actually relied on the old behavior of link_to(active_record_model) where Model#to_s was used for the link name and the URL was set to url_for({}) which returns the current path. I didn't see any mention of this in the upgrade guide or release notes, but I found this by looking at the git history for link_to. 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is in the changelog. Not everything from the CHANGELOG makes to those notes. Only things that would impact most of people and that need explanation how to solve. But, I think we can include this one. Do you want to open a PR to add it to the release notes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafaelfranca I think the current judgment is sound, this probably doesn't impact many people as there wasn't really a good reason to intentionally do link_to(active_record_model) before and expect the current path instead of the model's path. Also Rails 7 is several years old now, I think if no one else encountered this first it's unlikely to come up again.
I really was just trying to share what I thought was a funny moment about working with weird legacy code, not trying to criticize any decision made by Rails. Thank you for your work!
Summary
Ever since I've started using Rails I've yearned to be able to link to models like this:
And have the following rendered:
This PR makes this a reality. And you'll no longer have to type the following if your model defines a
to_smethod:Or even:
Other Information
I initially implemented this a Railtie plugin gem but the idea of touching the internals of
url_forin a gem gives me the heebee jeebees as you might understand. I'd much rather this exist as an option, even if rarely used, within Action View than within a likely soon-to-be out-of-date gem.I'm aware this amounts to me telling the Rails team to maintain this neato feature, but I'll gladly support maintenance.