16

Is there any way to prevent validation messages appearing twice for Paperclip upload validations?

Here is my model:

has_attached_file :photo, :styles => { :thumb => "215x165" }, :default_url => "/images/:style/missing.png"

validates_attachment :photo, :presence => true,
:content_type => { :content_type => "image/jpg" },
:size => { :in => 0..0.5.megabytes }

Here is my view:

<% if @product.errors.any? %>
<p>The following errors were found:</p>
  <ul>
    <% @product.errors.full_messages.each do |message| %>
      <li>- <%= message %></li>
    <% end %>
  </ul>
<% end %>

If I upload an invalid file I get the following error messages:

  • Photo content type is invalid
  • Photo is invalid

Is there any way to make just one of these show up? I have tried adding message: to the model. But then this just comes up twice too!

Thank you!

jonny_FIVE
  • 457
  • 1
  • 4
  • 12

3 Answers3

25

If you inspect the @model.errors hash, you can see it returns an array for the :photo attribute, and a message for each paperclip validator.

{:photo_content_type=>["is invalid"], 
 :photo=>["is invalid", "must be less than 1048576 Bytes"], 
 :photo_file_size=>["must be less than 1048576 Bytes"] }

You'll need to filter one lot of them with a bit of Ruby. There are many ways to go about it (see here for some ideas), but a quick fix could be to delete the :photo array and use just the messages from paperclip generated attributes.

@model.errors.delete(:photo)

This should leave you with a @model.errors.full_messages like this:

["Photo content type is invalid", "Photo file size must be less than 1048576 Bytes"]
Community
  • 1
  • 1
Jimeux
  • 2,956
  • 1
  • 18
  • 14
  • @Jimeux, your answer is correct in current circumstances however I've opened up a PR for the actual solution of this issue. Upvote that issue if you want this solution merged into paperclip https://github.com/thoughtbot/paperclip/pull/1418 – Kashif Umair Liaqat Feb 03 '14 at 07:02
15

In my opinion, below is a better solution

class YourModel < ActiveRecord::Base
  ...

  after_validation :clean_paperclip_errors

  def clean_paperclip_errors
    errors.delete(:photo)
  end
end

See the comment by @rubiety here

Hoa
  • 3,179
  • 1
  • 25
  • 33
3

Note that solutions from previous answers work well until you don't need the presence validation. That is because @model.errors.delete(:photo) will remove duplicates as well as your presence validation error. Code below retains validation errors for attributes specified as argument to retain_specified_errors method.

class YourModel < ActiveRecord::Base
  ...

  after_validation {
    retain_specified_errors(%i(attr another_att))
  }

  def retain_specified_errors(attrs_to_retain)
    errors.each do |attr|
      unless attrs_to_retain.include?(attr)
        errors.delete(attr)
      end
    end
  end
end
Jeremen
  • 161
  • 1
  • 9