ruby-on-rails-5 - 回形针内容大小验证不起作用
问题描述
我已经使用回形针设置了一个用户管理应用程序,我想在他或她的个人资料更新中上传用户的 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 的文件。
解决方案
推荐阅读
- ms-access - 我在一行中有多个 ID(最多 20 个)。每个 ID 指向该行中的每个其他 ID。我需要 2 列所有 ID 之间的所有关系
- apache-kafka-streams - Spring @StreamListener 进程(KStream流)分区
- python - 代码是理解 Vae 与标准自动编码器的正确方式吗?
- json - Swift Tableview 数据加载使索引超出范围
- java - @JsonSerializer 在从 v1 Jackson 迁移到 2.4.4 后无法正常工作,就像它被忽略一样
- graph - 在 Neo4j 中使用 3d 坐标创建节点
- image - 计算每个像素的灰度图像标准差
- css - 仅使用 CSS 具有动态宽度的多个粘性(固定)表格列?
- r - 在 R 中估算缺失值
- mpdf - 表格以 pdf 格式显示,仅逐行显示一个字符