Class: Motor::ActiveStorageAttachmentsController

Inherits:
ApiBaseController
  • Object
show all
Includes:
WrapIoParams
Defined in:
app/controllers/motor/active_storage_attachments_controller.rb

Instance Method Summary collapse

Methods included from CurrentAbility

#current_ability

Methods included from CurrentUserMethod

#current_user

Instance Method Details

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/motor/active_storage_attachments_controller.rb', line 13

def create
  blob = if file_params[:key]
           ActiveStorage::Blob.create!(file_params)
         else
           ActiveStorage::Blob.create_and_upload!(file_params)
         end

  @attachment.assign_attributes(blob: blob, record: record)
  @attachment.assign_attributes(record_type: '', record_id: 0) unless record

  if @attachment.save(validate: false)
    render json: { data: Motor::ApiQuery::BuildJson.call(@attachment, params, current_ability) }
  else
    render json: { errors: @attachment.errors }, status: :unprocessable_entity
  end
end