File downloaded from url with Carrierwave but "nil" field in my model - ruby-on-rails

I'm integrating a video section in my small CMS.
I create a new video, I put the url in a form input field and then I must save the video url and also a thumbnail. Uusually videos are from Vimeo and Youtube.
I'm using CarrierWave to download thumb from url.
My Video model is this (simplified)
class Video < ActiveRecord::Base
mount_uploader :thumb_url, VideoThumbUploader
before_save :save_thumb_url
private
def save_thumb_url
self.remote_thumb_url_url = extract_thumb
end
def extract_thumb
thumb_url = if is_youtube_video?
youtube_thumb_url
elsif is_vimeo_video?
vimeo_thumb_url
else
'http://placehold.it/100x100'
end
end
end
In my folder I have the downloaded thumbnail, but the thumb_url column of my db is empty: when I save my form _thumb_url is NIL.
Why?

https://github.com/carrierwaveuploader/carrierwave/issues/1078
It's an issue. Here the workaround!

Related

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.
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.

Rails Active Storage: Get relative disk service path for attachment

I'm switching to Rails Active Storage to handle upload and storage of images locally (using the disk service) for a product catalog, and I'm having trouble getting a usable url to the image to feed into an <img> tag. I'm using React on the frontend, so I can't (easily) use Rails helpers to generate the tag.
ActiveStorage puts the files in /public/images. I can hardcode relative links to the files (i.e. http://localhost:3000/images/Ab/CD/AbCDEfGhIjkL) and it works fine.
Relevant snippet from Product.rb:
class Product < ApplicationRecord
attr_accessor :image_url
has_one_attached :image
def as_json(options)
h = super(options)
if self.image.attached?
h[:image_url] = ActiveStorage::Blob.service.service_url(self.image.key)
end
h
end
end
as_json produces a JSON object to feed to React that has an entry image_url that is used for the <img>'s src attribute. With the code above, image_url contains the full path to the file (i.e. http://localhost:3000/srv/www/rails_app/public/images/Ab/CD/AbCDEfGhIjkL). Using url_for in the view produces the same result. I want it to only contain the path relative to rails root.
I could manipulate the string to remove everything before the relative path, but I foresee this causing bugs in the future if anything ever changes, so I'd much rather find a way to get ActiveStorage to just generate an appropriate string for me.
Thanks!
You need to use the routes helper to build of a URL to your Rails app.
https://guides.rubyonrails.org/active_storage_overview.html#linking-to-files
class Product < ApplicationRecord
attr_accessor :image_url
has_one_attached :image
def as_json(options)
h = super(options)
if self.image.attached?
h[:image_url] = Rails.application.routes.url_helpers.rails_blob_path(self.image)
end
h
end
end

carrierwave multiple file uploads and storing

I want to do a multiple file upload with carrierwave.
When I upload I transcode a movie in sveral formats .mp4 .mov ...
Now I want to upload all those and store them in DB?
how can I save versions of a file with carrierwave?
thanks
Add the relevant attributes to your model and introduce a before_save callback.
class Video < ActiveRecord::Base
mount_uploader :video, VideoUploader
before_save :update_video_attributes
private
def update_video_attributes
if video.present? && video_changed?
self.content_type = video.file.content_type
self.file_size = video.file.size
end
end
end
For more details see github

Carrierwave upload - image or video

I have found this link that explains how not to make thumbnails if the upload is not a video.
But how can I check, afterwards, if the saved file IS or IS NOT an image ? (In my case, I can store either images or videos), by calling a method on the model that holds the uploader ? DO I need some sort of callback in is_image?(new_file) that will set a field on the model ?
I'd like to be able to call #model.is_image?or #model.iI would bes_video? or maybe even something like case #model.type; when :video ; ...
I have added a callback to set an attribute on the model that holds the uploader
# uploader/my_uploader.rb
...
def image?(new_file)
if new_file.content_type.include? 'image'
model.image = true
true
else
false
end
end
# models/my_model.rb (mongoid)
...
field :image, type: Boolean
mount_uploader MyUploader
# View
#my_model.image?

RoR not creating nested record on before_save

I have a model Site which has_many photos.
In site model I have
before_save :defaults
def defaults
if self.photos.length ==0 && !SiteDefault.default_photo.nil?
photo = Photo.new
photo.image = SiteDefault.default_photo.image
self.photos.push photo
end
end
It is not storing the default photo when creating a site. Why?
I dont see any error in the console. The image is being uploaded to Amazon S3 correctly.

Resources