views:

300

answers:

3

I use paperclip to attach an avatar to users, which works fine but when a new user attempts to register it complains about the avatar bieng too small and not of the right type.

This is how i validate my avatars:

validates_attachment_size :avatar, :less_than => 1.megabytes
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png', 'image/gif']

This is the error i get when i try to register.

There were problems with the following fields:

* Avatar file size file size must be between 0 and 1048576 bytes.
* Avatar content type is not included in the list

is there anyway to make it so that avatar can be blank?

+1  A: 

I don't know if this is gonna work but try:

validates_attachment_size :avatar, :less_than => 1.megabytes, :if => avatar_changed?
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png', 'image/gif'], :if => avatar_changed?
khelll
I used ":unless => :new_user?" and wrote a function that returns true/false which seems to have fixed it
Arcath
A: 

I didn't use paperclip, but in general in Rails you can add condition to decide if validations should be run.

validates_attachment_size :avatar, 
  :less_than => 1.megabytes, 
  :unless => "avatar.blank?"

You should add similar condition to all validations that affects avatar. If you want know more take a look here.

klew
A: 

It's more like:

validates_attachment_size :avatar, :less_than => 1.megabytes, :unless => "avatar_content_type.blank?"

Tiago Franco