Ruby on Rails 4 - Duplicate paperclip validation messages

不问归期 提交于 2019-12-18 12:55:09

问题


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!


回答1:


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"]



回答2:


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




回答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


来源:https://stackoverflow.com/questions/19933579/ruby-on-rails-4-duplicate-paperclip-validation-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!