Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails 4 - Duplicate paperclip validation messages

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!

like image 837
jonny_FIVE Avatar asked Nov 12 '13 15:11

jonny_FIVE


3 Answers

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"]
like image 123
Jimeux Avatar answered Nov 05 '22 20:11

Jimeux


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

like image 15
Hoa Avatar answered Nov 05 '22 22:11

Hoa


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
like image 3
Jeremen Avatar answered Nov 05 '22 21:11

Jeremen