3

So I've been trying to fix this for a while now and I've had no luck in doing so. I have a model Posts that has_rich_text: body

class Post < ApplicationRecord
  extend FriendlyId
  friendly_id :title, use: :slugged
  has_rich_text :body
  has_rich_text :health_check
  has_one_attached :cover_photo
  has_many :post_tags, dependent: :destroy
  has_many :tags, through: :post_tags

  after_commit :add_default_cover, on: [:create, :update]

  def add_default_cover
    unless cover_photo.attached?
      self.cover_photo.attach(io: File.open(Rails.root.join("app", "assets", "images", "default.png")), filename: 'default.png' , content_type: "image/png")
    end
  end
end

It works perfectly when I attach photos, but when ever I attach a gif it gets uploaded correctly into the edit/new screen and I can see the animation of the gif in the rich text editor. But as soon as I submit the edit/new form then a new variant is created as an image and that's what is being used when showing the post. When I check my storage system I find both the image and the gif versions.

Does anyone know why this is happening on submitting the form? I would want to upload a gif and display it without active stroage or action text changing it.

Faisal Choura
  • 170
  • 1
  • 8

1 Answers1

3

I had this same issue. I ended up hacking it on the client-side with JS.

My approach: on 'DOMContentLoaded' event for the page with the GIF/MP4s, I grab all the action text attachments, get their URLS, and then for the GIFs I replace the static image URL with the GIF URL, and for the MP4s I just create a new <video> element and give it the MP4 URL. It's not pretty, but it works.

Here is an example on my blog. Let me know if you find a better answer, this feels like something that should be built into action storage/text.

  • 1
    I'm really surprised there isn't a lot more discussion about this, and your answer had no upvotes. – Bashar Abdullah Jul 14 '20 at 15:25
  • 1
    @BasharAbdullah I think most people are waiting for the Rails/Action Text team to make additional improvements. There is a GitHub [issue](https://github.com/rails/rails/issues/35218#issuecomment-647315522) where there is a discussion about ActionText attachments. – André Jessee Jul 15 '20 at 16:46
  • 1
    @AndréJessee based on the Rails/Basecamp's attitude I think we'll be waiting a long time for meaningful improvements. I've moved on to a different WYSIWYG library. – yakattack Feb 28 '21 at 00:42
  • @yakattack what did you move to? – André Jessee Mar 01 '21 at 17:45