Test UploadsController

This commit is contained in:
Jose Farias
2024-11-29 18:40:58 -06:00
parent 2b9c4d9b71
commit 22d59ba4fb
2 changed files with 42 additions and 5 deletions
+17 -5
View File
@@ -1,16 +1,28 @@
class UploadsController < ApplicationController
include ActiveStorage::SetCurrent
before_action :set_file, only: :create
before_action :set_attachment, only: :show
def create
file = params[:file]
blob = ActiveStorage::Blob.create_and_upload! io: file, filename: file.original_filename, content_type: file.content_type
@upload = Current.account.uploads_attachments.create! blob: blob
render :create, status: :created, formats: :json
@upload = Current.account.uploads_attachments.create! blob: create_blob!
end
def show
@attachment = ActiveStorage::Attachment.find_by! slug: "#{params[:slug]}.#{params[:format]}"
expires_in 1.year, public: true
redirect_to @attachment.url
end
private
def set_file
@file = params[:file]
end
def set_attachment
@attachment = ActiveStorage::Attachment.find_by! slug: "#{params[:slug]}.#{params[:format]}"
end
def create_blob!
ActiveStorage::Blob.create_and_upload! io: @file, filename: @file.original_filename, content_type: @file.content_type
end
end