Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Storage - Adding File Description / Text - Ruby on Rails 5.2

With the release of Rails 5.2, the much used Paperclip gem is now deprecated and it's advised to use Active Storage that ships with Rails. I'm starting a new project and set up Active Storage with ease, but my problem comes when trying to add a name or description to the file uploads.

With Paperclip I would add a column to the model called something like file_upload_name, so that as well as having a file name "something.pdf" I could also add a name or description such as "My Important Document" on the upload form.

For the projects that I'm doing, this is a vital part of the upload process and ideally needs to be done at the time of upload. As Active Record doesn't store to a model in such a way it's not as simple as just adding a column and adding fields to a form. It seems something that should be relatively simple but I can't figure it out or find any information about how best to do it. Any help much appreciated.

Here's an example of what I'm trying to achieve:

enter image description here

With Active Storage the end result is a multiple file upload button, with no naming etc.

like image 779
DanRio Avatar asked Jan 31 '26 23:01

DanRio


1 Answers

You should create a new model to wrap each attached file. That model would then have the ActiveStorage attachment defined on it, as well as whatever other attributes you need to capture. Ex:

class Attachment < ApplicationRecord
  has_one_attached :file
end

Rails then treats file kind of like an attribute for each Attachment. You can define your other attributes (e.g. upload_name, etc.) on the Attachment model. Based on your screenshot, it looks like maybe a Quotation has many attached files, so you'd do something like:

class Quotation < ApplicationRecord
  has_many :attachments
end
like image 81
Jim Ryan Avatar answered Feb 03 '26 16:02

Jim Ryan