首页 > 解决方案 > 回形针内容大小验证不起作用

问题描述

我已经使用回形针设置了一个用户管理应用程序,我想在他或她的个人资料更新中上传用户的 aadhar 和 pan 卡。它工作正常,但是当我上传违反验证的文件时,它没有显示错误并且文件正在成功上传。

用户.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

控制器动作:

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

它也接受超过 1 MB 的文件。

标签: ruby-on-rails-5paperclip-validation

解决方案


推荐阅读