Skip to content
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

Fix link_to with a model passed as an argument twice #43918

Merged
merged 1 commit into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions actionview/lib/action_view/helpers/url_helper.rb
Expand Up @@ -208,7 +208,6 @@ def _filtered_referrer # :nodoc:
#
def link_to(name = nil, options = nil, html_options = nil, &block)
html_options, options, name = options, name, block if block_given?
options ||= {}

html_options = convert_options_to_data_attributes(options, html_options)

Expand Down Expand Up @@ -743,7 +742,7 @@ def convert_options_to_data_attributes(options, html_options)
end

def url_target(name, options)
if name.respond_to?(:model_name) && options.empty?
if name.respond_to?(:model_name) && options.is_a?(Hash) && options.empty?
url_for(name)
else
url_for(options)
Expand Down
10 changes: 8 additions & 2 deletions actionview/test/template/url_helper_test.rb
Expand Up @@ -16,7 +16,7 @@ def persisted?
end

def to_s
id.to_s
"Workshop #{id}"
end
end

Expand Down Expand Up @@ -621,7 +621,13 @@ def test_link_tag_does_not_escape_html_safe_content
def test_link_tag_using_active_record_model
@workshop = Workshop.new(1.to_s)
link = link_to(@workshop)
assert_dom_equal %{<a href="/workshops/1">1</a>}, link
assert_dom_equal %{<a href="/workshops/1">Workshop 1</a>}, link
end

def test_link_tag_using_active_record_model_twice
@workshop = Workshop.new(1.to_s)
link = link_to(@workshop, @workshop)
assert_dom_equal %{<a href="/workshops/1">Workshop 1</a>}, link
end

def test_link_to_unless
Expand Down