这方面有什么建议吗?
partner.rb
LOGO_VALIDATIONS = {
max_size: 2.megabytes
}
has_one_attached :logo
validate :validate_logo_attachment
def validate_logo_attachment
if logo.attached? && logo.blob.byte_size > LOGO_VALIDATIONS[:max_size]
errors.add(:logo, 'must be less than 2 Mb')
end
end
partners_controller.rb
def change_logo
@current_partner.logo.purge
@current_partner.logo.attach(change_logo_params[:logo])
render_error(@current_partner.errors.full_messages) && return unless @current_partner.valid?
@current_partner.save!
render_success({logo_url: @current_partner.logo_url})
end
当我尝试更改超过2 Mb的徽标图像时,出现以下预期错误。
{
"result": "failed",
"messages": [
"Logo must be less than 2 Mb"
]
}
但是当我用rails_blob_path(@current_partner.logo, disposition: "attachment", only_path: true)
检查图像时,会得到新的徽标图像。