I have set up an user management app using paper clip and I want to upload user's aadhar and pan card on his or her profile update. Its working fine but when I upload file which violates the validations, its not showing errors and files are getting uploaded successfully.
user.rb:
has_attached_file :aadhar,
style: { :medium => "300x300>", thumb: "100x100>" }
validates_attachment_content_type :aadhar, :content_type => /\Aimage\/(jpg|jpeg|pjpeg|png|x-png)\z/, :message => 'is not allowed (only jpeg/jpg/png images are allowed)'
validates_attachment_size :aadhar, :less_than => 1.megabyte
has_attached_file :pan_card,
style: { :medium => "300x300>", thumb: "100x100>" }
validates_attachment_content_type :pan_card, :content_type => /\Aimage\/(jpg|jpeg|pjpeg|png|x-png)\z/, :message => 'is not allowed (only jpeg/jpg/png images are allowed)'
validates_attachment_size :pan_card, :less_than => 1.megabyte
Controller action:
def upload
current_user.last_activity_at = Time.now
if params[:aadhar][:base64] != nil
image_base = params[:aadhar][:base64]
image = Paperclip.io_adapters.for(image_base)
image.original_filename = params[:aadhar][:filename]
current_user.aadhar = image
current_user.aadhar_status = "pending"
end
if params[:pan_card][:base64] != nil
image_base = params[:pan_card][:base64]
image = Paperclip.io_adapters.for(image_base)
image.original_filename = params[:pan_card][:filename]
current_user.pan_card = image
current_user.pan_card_status = "pending"
end
if params[:pan_number] != nil
current_user.pan_number = params[:pan_number]
end
if current_user.save
render( json: UserSerializer.send_obj("success").to_json)
else
render( json: UserSerializer.response_error(current_user.errors.full_messages).to_json, status: 422 )
end
end
It is accepting files more than 1 MB also.