活动存储可保存附件,即使发生错误

时间:2019-03-10 11:03:20

标签: rails-activestorage ruby-on-rails-5.2

这方面有什么建议吗?

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)检查图像时,会得到新的徽标图像。

0 个答案:

没有答案