سؤال

Rails version: 4.1
Paperclip version: 4.1.1

I am using an Upload model to handle my attachments for all other models, since in most cases:

model.rb:
has_many :uploads
validates_associated :uploads

Currently, there are two validations for my paperclip attachment. Content:

# list of file types presented as ... for demonstration only
validates_attachment :attachment, content_type: { content_type:[                         
           "application/pdf",
           "application/vnd.ms-excel","application/excel",
           "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
           "application/vnd.oasis.opendocument.spreadsheet",
           "application/vnd.oasis.opendocument.text",
           "application/vnd.oasis.opendocument.presentation",
           "application/msword", "text/rtf", "text/plain",
           "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
           "application/mspowerpoint",
           "application/vnd.openxmlformats-officedocument.presentationml.presentation",
           "application/x-rar-compressed", "application/x-rar", "application/zip"]}

and a custom validation which runs a clamdscan (virus check):

validate :clean_file

def clean_file
    errors.add :base, "File #{self.attachment.queued_for_write.inspect} is infected!" if
    !self.attachment.queued_for_write.blank? &&
    ("#{system "clamdscan - < \"#{self.attachment.queued_for_write[:original].path}\" | grep FOUND" }")
end

Both validations work as expected. The error however always falls to "upload is invalid" or whatever string I provide in my locales file.

errors.add :base seems to be ignored.

Edit: The error message displayed is from Model, not from Upload.

How can I pass an error message that actually gets displayed????

Thanks

Edit2: Added the content validation string

SOLVED: Using this approach
validates associated with model's error message

هل كانت مفيدة؟

المحلول

I posted above that this is issue is now solved.

This is partially true. The above solution only worked in rendering if the associated object was supplied first in the list.
So instead of using this solution I tried a new one, on view level this time, since errors are produced but not rendered, and it worked well.

Rendering an _errors partial like this ("junk" code is removed):

<% if current_object.errors.any? %>
    <ul>
        <% current_object.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
        <% end %>
        <% if current_object.uploads.any? %>
            <% current_object.uploads.each do |u| %>
                <% u.errors.full_messages.each do |umsg| %>
                    <li><%= umsg %></li>
                <% end %>
            <% end %>
        <% end %>
    </ul>
<% end %>

I get all my errors displayed. At last!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top