Last active
February 4, 2023 22:51
-
-
Save shayani/c7f1e95867a7efdc775318c35d01533b to your computer and use it in GitHub Desktop.
Fix missing images on Trix when upgrading from Rails 6.1 to 7.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fix missing images on Trix when upgrading from Rails 6.1 to 7.0 | |
# Source: https://discuss.rubyonrails.org/t/rails-6-1-7-gives-404-for-actiontext-attachments/80803 | |
def refresh_trix(trix) | |
return unless trix.embeds.size.positive? | |
trix.body.fragment.find_all("action-text-attachment").each do |node| | |
embed = trix.embeds.find { |attachment| attachment.filename.to_s == node["filename"] && attachment.byte_size.to_s == node["filesize"] } | |
next unless embed.try(:blob) | |
node.attributes["url"].value = Rails.application.routes.url_helpers.rails_storage_redirect_url(embed.blob, host: "eventaservo.org") | |
node.attributes["sgid"].value = embed.attachable_sgid | |
end | |
trix.update_column :body, trix.body.to_s | |
end | |
ActionText::RichText.where.not(body: nil).find_each do |trix| | |
refresh_trix(trix) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment