Skip to content

Instantly share code, notes, and snippets.

@michalkorzawski
Last active October 30, 2022 22:04
Show Gist options
  • Save michalkorzawski/bf487868d1cd48be02e1aa55e8dc9b3e to your computer and use it in GitHub Desktop.
Save michalkorzawski/bf487868d1cd48be02e1aa55e8dc9b3e to your computer and use it in GitHub Desktop.
# app/controllers/records_controller.rb
def some_method
... business logic ...
attaching_images = params[:record][:images].presence
if attaching_images
attaching_images = attaching_images.map do |im|
tempfile = ImageConverter.new(tempfile: im.tempfile, width: 1200).call
ActionDispatch::Http::UploadedFile.new(
{
filename: "#{im.original_filename.split('.').first}.jpg",
tempfile: tempfile,
type: 'image/jpg',
head: "Content-Disposition: form-data; name=\"property[images][]\"; filename=\"#{im.original_filename.split('.').first}.jpg\"\r\nContent-Type: image/jpg\r\n"
}
)
end
end
if record.new(permitted_params.merge(images: attaching_images)).save
...
else
...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment