In Rails 7.0 and earlier, we customized the Trix editor to embed YouTube videos. As is customary, the video will be embedded via an iframe tag. The Rails sanitizer removes this tag as it can be abused to embed malicious websites.
Allowing the tag in config/initializers/action_text.rb used to do the trick.
Rails.application.config.after_initialize do
ActionText::ContentHelper.allowed_tags << "iframe"
end
Rails 7.1 has a new HTML5 sanitizer, but the old HTML4 one can still be used. This means ActionText::ContentHelper
is not yet defined when the application boots, so the snippet above will crash the server. I expected the after_initialize
to wait long enough for the sanitizer to be loaded, but no dice.
Unable to load application: NoMethodError: undefined method `<<' for nil:NilClass
config/initializers/action_text.rb:2:in `block in <top (required)>': undefined method `<<' for nil:NilClass (NoMethodError)
Any ideas on how to allow sanitized tags in Rails 7.1?
I was stuck with the same problem. According to ActionView 7.1.0 CHANGELOG it's nil until is set in the application.
So, I decided to copy all tags from 'rails-html-sanitizer' gem to config/initializers/action_text.rb and also added my specific tags there
It did the job.