I've model Profile
class Profile < Abstract
has_attached_file :avatar,
...
validates_attachment_size :avatar, :less_than => 2.megabytes
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png', ...]
# Many other validations
end
I have two different forms: one for avatar and another for all other fields. User have to be able to save avatar without filling second form. Is it possible to validate only paperclip attachment, skipping all other validations? Following this answer I tried to do like this:
class Abstract < ActiveRecord::Base
def self.valid_attribute?(attr, value)
mock = self.new(attr => value)
unless mock.valid?
return !mock.errors.has_key?(attr)
end
true
end
end
and in controller
def update_avatar
if params[:profile] && params[:profile][:avatar].present? && Profile.valid_attribute?(:avatar, params[:profile][:avatar])
@profile.avatar = params[:profile][:avatar]
@profile.save(:validate => false)
...
else
flash.now[:error] = t :image_save_failure_message
render 'edit_avatar'
end
end
But it didn't work for paperclip. Profile.valid_attribute?(:avatar, params[:profile][:avatar])
always returns true.