file_validators icon indicating copy to clipboard operation
file_validators copied to clipboard

Reform + Paperclip + S3

Open rapides opened this issue 8 years ago • 3 comments

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?

rapides avatar Aug 21 '17 10:08 rapides

Does this work without the validations of image attributes?

musaffa avatar Aug 21 '17 11:08 musaffa

Nope, still get the same error.

rapides avatar Aug 22 '17 06:08 rapides

Then it's not probably related to file_validators gem. For file uploading I highly recommend Shrine over Paperclip.

musaffa avatar Aug 22 '17 06:08 musaffa