Skip to content

Instantly share code, notes, and snippets.

@shayani
Last active February 4, 2023 22:51
Show Gist options
  • Save shayani/c7f1e95867a7efdc775318c35d01533b to your computer and use it in GitHub Desktop.
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
# 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