13

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
sawa
  • 165,429
  • 45
  • 277
  • 381
dtothefp
  • 1,364
  • 1
  • 16
  • 22

3 Answers3

34

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

scottknight
  • 442
  • 4
  • 6
  • You are right @savmac, order is important. I has same issue, but I remember I read somewhere that, associations should always before validation. – egyamado Nov 25 '13 at 03:12
  • Paperclip could throw better error in case it fails to find attachment passed to `validates_attachment`! – Ev Dolzhenko Feb 10 '14 at 14:56
4

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.

Sergey Pedan
  • 179
  • 1
  • 7
1

savmac's fix worked for me just now. i was having the same problem upon heroku open. the lines in the model had been out of order previously and my app had worked seamlessly for months--dunno what changed. thanks!

SOConnell
  • 793
  • 1
  • 8
  • 27