Chrismisballs
Chrismisballs

Reputation: 320

Rails ActionText embends 404

Moved app from heroku to render. Everthing seems to be working fine onces ENV where set correctly.

However my actionText embeds from heroku seem to be broken. Any new embeds on render are fine.

I have RAILS_MASTER_KEY = the value in credientals.yml for secret_key_base set in environments tab correctly.

Inspecting the Response Headers there is no location

Anyone run into this issue? Got any ideas?

Upvotes: 0

Views: 154

Answers (1)

Chrismisballs
Chrismisballs

Reputation: 320

So no more than a couple hours later and googling I found a solution. I found two sources that helped me solve this problem: How To Update ActionText Attachments and Rails 6.1 -> 7 gives 404 for ActionText attachments

Reading the Rails 6.1 thread a person created a couple methods to fix this problem:

  # After active storage urls are changed, use this to recreate all trix attachments
  def self.refresh_trixes
    ActionText::RichText.where.not(body: nil).find_each do |trix|
      refresh_trix(trix)
    end
  end

  # After active storage urls are changed, use this to recreate a specific trix attachments
  def self.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"] }

      node.attributes["url"].value = Rails.application.routes.url_helpers.rails_storage_redirect_url(embed.blob, host: "YOUR_DOMAIN")
      node.attributes["sgid"].value = embed.attachable_sgid
    end

    trix.update_column :body, trix.body.to_s
  end

This is where gorails github comes into play, using this rake task minus the user statement, I created a rake task in app/lib/taks/action_text.rb:

namespace :action_text do
  task refresh_embeds: :environment do
    ActionText::RichText.where.not(body: nil).find_each do |trix|
      next 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"] }

    # Files
      if embed.present?
        node.attributes["url"].value = Rails.application.routes.url_helpers.rails_storage_redirect_url(embed.blob, host: "My_Domain")
        node.attributes["sgid"].value = embed.attachable_sgid

    # User mentions
      #elsif (user = User.find_by(id: Base64.decode64(node["sgid"])[/User\/(\d+)/, 1]))
        #node.attributes["sgid"].value = user.attachable_sgid
      end
    end

      trix.update_column :body, trix.body.to_s
    end
  end
end

My_Domain = replace with your site Then from the render's shell either via ssh or on the dashboard I ran the following command: rails action_text:refresh_embeds RAILS_ENV=prouction

Presto all embeds are located in the redirect and rendered in view.

Upvotes: 1

Related Questions