Skip to content

Commit 8e86e87

Browse files
committed
Merge PR rails#42234
2 parents 1cc7e69 + 1e9629e commit 8e86e87

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

actionview/CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
* Allow `link_to` helper to infer link name from `Model#to_s` when it
2+
is used with a single argument:
3+
4+
link_to @profile
5+
#=> <a href="/profiles/1">Eileen</a>
6+
7+
This assumes the model class implements a `to_s` method like this:
8+
9+
class Profile < ApplicationRecord
10+
# ...
11+
def to_s
12+
name
13+
end
14+
end
15+
16+
Previously you had to supply a second argument even if the `Profile`
17+
model implemented a `#to_s` method that called the `name` method.
18+
19+
link_to @profile, @profile.name
20+
#=> <a href="/profiles/1">Eileen</a>
21+
22+
*Olivier Lacan*
23+
124
* Support svg unpaired tags for `tag` helper.
225

326
tag.svg { tag.use('href' => "#cool-icon") }

actionview/lib/action_view/helpers/url_helper.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def _filtered_referrer # :nodoc:
8585
# # name
8686
# end
8787
#
88+
# link_to(active_record_model)
89+
#
8890
# ==== Options
8991
# * <tt>:data</tt> - This option can be used to add custom data attributes.
9092
# * <tt>method: symbol of HTTP verb</tt> - This modifier will dynamically
@@ -137,6 +139,12 @@ def _filtered_referrer # :nodoc:
137139
# link_to nil, "http://example.com"
138140
# # => <a href="http://www.example.com">http://www.example.com</a>
139141
#
142+
# More concise yet, when +name+ is an Active Record model that defines a
143+
# +to_s+ method returning a default value or a model instance attribute
144+
#
145+
# link_to @profile
146+
# # => <a href="http://www.example.com/profiles/1">Eileen</a>
147+
#
140148
# You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
141149
#
142150
# <%= link_to(@profile) do %>
@@ -204,7 +212,7 @@ def link_to(name = nil, options = nil, html_options = nil, &block)
204212

205213
html_options = convert_options_to_data_attributes(options, html_options)
206214

207-
url = url_for(options)
215+
url = url_target(name, options)
208216
html_options["href"] ||= url
209217

210218
content_tag("a", name || url, html_options, &block)
@@ -719,6 +727,14 @@ def convert_options_to_data_attributes(options, html_options)
719727
end
720728
end
721729

730+
def url_target(name, options)
731+
if name.respond_to?(:model_name) && options.empty?
732+
url_for(name)
733+
else
734+
url_for(options)
735+
end
736+
end
737+
722738
def link_to_remote_options?(options)
723739
if options.is_a?(Hash)
724740
options.delete("remote") || options.delete(:remote)

actionview/test/template/url_helper_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class UrlHelperTest < ActiveSupport::TestCase
3535
get "/other" => "foo#other"
3636
get "/article/:id" => "foo#article", :as => :article
3737
get "/category/:category" => "foo#category"
38+
resources :workshops
3839

3940
scope :engine do
4041
get "/" => "foo#bar"
@@ -511,6 +512,12 @@ def test_link_tag_does_not_escape_html_safe_content
511512
link_to(raw("Malicious <script>content</script>"), "/")
512513
end
513514

515+
def test_link_tag_using_active_record_model
516+
@workshop = Workshop.new(1.to_s)
517+
link = link_to(@workshop)
518+
assert_dom_equal %{<a href="/workshops/1">1</a>}, link
519+
end
520+
514521
def test_link_to_unless
515522
assert_equal "Showing", link_to_unless(true, "Showing", url_hash)
516523

0 commit comments

Comments
 (0)