I have googled/stack overflowed for hours and not found a solution to this problem. I'm wondering if my installation of PaperClip was somehow unsuccessful. I'm trying to validate an image attachment in my models folder:
validates :image, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']},
size: { less_than: 5.megabytes }
I have also tried code more similiar to the read me file on github:
validates_attachment :image, :presence => true,
:content_type => { :content_type => 'image/jpeg', 'image/jpg', 'image/png', 'image/gif' },
:size => { less_than: => 5.megabytes }
And I've tried to use individual validations
validates_attachment_presence :image
validates_attachment_content_type :image,:content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']
validates_attachment_size :image,:less_than => 5.megabytes
I get an error in all cases. Either:
Routing Error
undefined method `before_image_post_process' for #<Class:0x00000101461750>
Try running rake routes for more information on available routes.
Or:
NoMethodError in PinsController#index
undefined method `key?' for nil:NilClass
Do you have has_attached_file :image
in your file?
If so, make sure it is before validates_attachment
.
I keep getting this error each time just because I always forget to rename the image variable the same way (after copying from the snippet):
has_attached_file :avatar...
validates_attachment_content_type :photo, :content_type...
→ Should be also :avatar
instead of :photo
It's a perfect example of how problems may arize when the code is not DRY.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With