Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionText gif attachment converts to photo when submitting rails form

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.

like image 945
Faisal Choura Avatar asked Dec 07 '25 12:12

Faisal Choura


1 Answers

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.

like image 187
André Jessee Avatar answered Dec 09 '25 03:12

André Jessee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!