Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails PaperClip Gem validate_attachement error

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
like image 450
dtothefp Avatar asked Jul 27 '13 18:07

dtothefp


2 Answers

Do you have has_attached_file :image in your file? If so, make sure it is before validates_attachment.

like image 163
scottknight Avatar answered Sep 19 '22 17:09

scottknight


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.

like image 38
Sergey Pedan Avatar answered Sep 20 '22 17:09

Sergey Pedan