I'm using Ruby on Rails 7 with the Turbo Rails gem, which provides the turbo_frame_tag method.
I would like to add default html data attributes to all the <turbo-frame> tags that are generated through turbo_frame_tag. That is, given the default data attribute to add is data-custom="value", I would like to generate the following HTML each time I use turbo_frame_tag:
<turbo-frame id="..." data-custom="value">...</turbo-frame>
How can I make that in an initializer?
It can be done like this... first define your custom attributes in an initializer:
Then override the Rails turbo_frame_tag view helper to include your custom attributes. I did this by including the following in app/helpers/application_helper.rb. There's probably a cleaner way, but I didn't take the time to find it!
This is just the Turbo view helper copied and changed to include your custom attributes.
An alternative, if you can live with it, is to define a helper called
custom_turbo_frame_tag, and add in your attributes (defined as above). This approach then calls the standard ActionView turbo_frame_tag helper, and so it might work better in case future Rails versions change the built-in helper.