Mayuresh Srivastava
Mayuresh Srivastava

Reputation: 1422

Carrierwave extension whitelist error message

I am using carrierwave with Rails 5. In my extension white list allowed types are jpg, jpeg and png.

The default error message is:

"You are not allowed to upload \"gif\" files, allowed types: jpg, jpeg, png"

I don't need customization as message is fine except \"gif\". What I want is:

"You are not allowed to upload gif file, allowed types: jpg, jpeg, png"

How to achieve this?

Upvotes: 1

Views: 2251

Answers (1)

Mayuresh Srivastava
Mayuresh Srivastava

Reputation: 1422

Answering based on solution suggested by @mizurnix. It worked for me.

Overrode the check_extension_whitelist! method in avatar uploader(app/uploaders/avatar_uploader.rb) i.e. added below code in avatar_uploader.rb.

private

  def check_extension_whitelist! new_file
    extension = new_file.extension.to_s
    if extension_whitelist && !whitelisted_extension?(extension)
      raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_whitelist_error", extension: extension, allowed_types: Array(extension_whitelist).join(", "))
    end
  end

  def whitelisted_extension? extension
    downcase_extension = extension.downcase
    Array(extension_whitelist).any? { |item| downcase_extension =~ /\A#{item}\z/i }
  end

Upvotes: 0

Related Questions