file_validators
file_validators copied to clipboard
Reform + Paperclip + S3
Let's consider following example:
# .../pitches_controller.rb
def upload_avatar
pitch = Pitch.find(params[:id])
form = PitchAvatarForm.new(pitch)
if form.validate(pitch_avatar_params)
form.save
on_pitch_avatar_upload_succeeded(form)
else
on_pitch_avatar_upload_fialed(form)
end
end
# .../forms/pitch_avatar_form.rb
class PitchAvatarForm < Reform::Form
model :pitch
property :image
property :image_file_name
property :image_content_type
property :image_file_size
validates :image,
file_content_type: { allow: ['image/jpeg', 'image/jpg', 'image/png'] },
file_size: { less_than: 2.megabytes }
end
On each form#save execution it couses following error:
form.save
NoMethodError: undefined method `sub' for nil:NilClass
from /Users/rapide/.rvm/gems/ruby-2.3.0@bttf/gems/paperclip-4.2.2/lib/paperclip/storage/s3.rb:255:in `s3_object'
I fixed this issue by assigning attributes to the pitch before creating the form but it looks like an ugly hack. Any idea how to solve this in better way?
Does this work without the validations of image attributes?
Nope, still get the same error.
Then it's not probably related to file_validators gem. For file uploading I highly recommend Shrine over Paperclip.