Thanks very much for this, it's very helpful to not have to worry about Rails forgetting what files have been uploaded in a form previously, and also having an easy way to delete uploads.
I'm a beginner and just wondering about the best way to incorporate this approach into an existing form and model (I'm creating Invoices and want the user to be able to attach scans of receipts in the Invoice form they're filling out, and be able to add new scans without having to upload existing ones again, and be able to delete any scans easily). I'm thinking of doing this using your approach to accepting nested attributes in my Invoice form with Stimulus JS (I got this from Episode 186, thanks!). Does this sound like a reasonable approach? I already use it for adding Line Items in the Invoice, and it seems like the same concept would work with this approach to Active Storage uploads, since each uploaded is its own post.
I guess I'm going to try it, unless anyone has a better idea. Otherwise, I'm not sure how I would associate these Active Storage instances with a parent (like Invoice, or Post, etc.) and doing it all in one convenient form.
This is SO much cleaner than the hack I tried to do that looked like this... Plus I also had other logic around project count track limits... I appreciate the tutorial :)
if files.length > 0
files.each do |f|
@project.tracks.create(mp3_file: ActiveStorage::Blob.find_signed(f), file_size_limit: file_size_limit)
end
end
Is there a way to configure Active Storage to use "uuid" as the type for :id, :blob_id, :record_id, etc. Using the MediaUploaderController, I want every medium to belong to the user that uploaded it. BUT my media isn't persisted because the ids are of type "bigint".
The only difference is that I have an Album and this album has_many Image model and this image model has_one attached :file
#image.rb
class Image < ApplicationRecord
belongs_to :album
has_one_attached :file
end
#album.rb
class Album < ApplicationRecord
has_many :images, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true, reject_if: :all_blank
has_one_attached :cover_photo
def self.attributable_params
[:title,
:cover_photo,
images:[],
]
end
end
#the form
<%= form_with url: album_images_uploads_path(@album), multiplart: true do |form| %>
<%= form.file_field :file, multiple: true, direct_upload: true %>
<%= form.submit %>
<% end %>
#images_uploads_controller.rb
class ImagesUploadsController < ApplicationController
before_action :set_album
def create
params[:file].each do |blob|
@album.images.create(file: blob)
end
redirect_to album_path(@album)
end
private
def set_album
@album = Album.find(params[:album_id])
end
end
I'm a beginner and just wondering about the best way to incorporate this approach into an existing form and model (I'm creating Invoices and want the user to be able to attach scans of receipts in the Invoice form they're filling out, and be able to add new scans without having to upload existing ones again, and be able to delete any scans easily). I'm thinking of doing this using your approach to accepting nested attributes in my Invoice form with Stimulus JS (I got this from Episode 186, thanks!). Does this sound like a reasonable approach? I already use it for adding Line Items in the Invoice, and it seems like the same concept would work with this approach to Active Storage uploads, since each uploaded is its own post.
I guess I'm going to try it, unless anyone has a better idea. Otherwise, I'm not sure how I would associate these Active Storage instances with a parent (like Invoice, or Post, etc.) and doing it all in one convenient form.
Thanks for the great episode!
This is SO much cleaner than the hack I tried to do that looked like this... Plus I also had other logic around project count track limits... I appreciate the tutorial :)
Is there a way to configure Active Storage to use "uuid" as the type for :id, :blob_id, :record_id, etc. Using the MediaUploaderController, I want every medium to belong to the user that uploaded it. BUT my media isn't persisted because the ids are of type "bigint".
The only difference is that I have an Album and this album has_many Image model and this image model has_one attached :file
this is what gets send from the form