Thursday, September 19, 2024 11:56:48 PM
> settings

Customize


Authenticate

> uploaded_files_controller.rb
# frozen_string_literal: true

class UploadedFilesController < AuthenticatedController
  before_action { authorize :uploaded_file }

  def index
    render action_name, locals: {
      files: UploadedFile.all
    }
  end

  def create
    number_of_files = 0

    create_file_params[:data][1..].each do |file|
      UploadedFile.create!(
        file_name: file.original_filename,
        file_type: file.content_type,
        data: file
      )

      number_of_files += 1
    end

    flash[:success] = "#{number_of_files} #{"file".pluralize(number_of_files)} uploaded"
    redirect_to files_path
  end

  def destroy
    file = UploadedFile.find_by(public_id: destroy_file_params[:id])
    raise NotFound if file.nil?

    file.destroy!

    respond_to do |format|
      format.turbo_stream { render action_name, locals: {file: file} }
      format.html { redirect_to :back }
    end
  end

  private

  def create_file_params
    params.require(:uploaded_file).permit(data: [])
  end

  def destroy_file_params
    params
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b