Allow dom_id method to accept multiple ids/models

Background

This proposal is related to a “regression” I encountered today with turbo-rails.

hotwired/turbo-rails/pull/476 removed the ability to pass multiple ids/models to the turbo_frame_tag helper method. In that PR it was suggested that this ability be passed down to the dom_id method (which is a part of rails care) method rather than the turbo_frame_tag helper (which is a part of turbo-rails).

The proposed method could presumably look something like this:

article = Article.new(id: 1)
comment = Comment.new(id: 1)

dom_id(article, comment)
# => "article_1_comment_1"

But I don’t think we could accept an unlimited amount of arguments while still preserving the current behavior regarding prefixes.

Currently the dom_id method accepts a prefix, which means method calls like the above result in a stringified representation of the ruby object.

dom_id(article, comment)
# => "#<Field:0x00000001179645c0>_article_1"

Proposal

Would it make sense to make the first argument in the dom_id method accept an array (similar to the cache helper from ActionView)? This would preserve the backwards compatibility of prefixes.

dom_id([article, comment])
# => article_1_comment_1

dom_id([article, comment], :my_prefix)
# => my_prefix_article_1_comment_1

# Still backwards compatible
dom_id(article)
# => article_1

I’m happy to work on a PR, but I wanted to see if I could drum up some support. Thank you reading!

My proposal would still change the method signature for turbo_frame_tag, but I think it’s still a good trade-off.

before

<%= turbo_frame_tag article, comment do %>
  <!--- code -->
<% end %>

after

<%= turbo_frame_tag [article, comment] do %>
  <!--- code -->
<% end %>
1 Like

I made a PR: allow dom_id method to accept an array of records_or_classes by jwilsjustin · Pull Request #50608 · rails/rails · GitHub

Feedback welcomed!