简体   繁体   中英

Paperclip fails on a validates_presence_of

Ok so i have a model and in that model i have

class User < ActiveRecord::Base
  validates_presence_of :name
  has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>",:tiny => "25x25>" }
end

when i create an user and upload a picture and not enter a name I get the expected message

1 error prohibited this user from being saved.
There were problems with the following fields:
    * Name can't be blank

But the image is gone...when i look at my console i see

        [paperclip] identify -format %wx%h '/var/folders/QK/QK+tCPT-Gx8yGDxVQbDuF++++TI/-Tmp-/stream20110113-19714-1ucz75p-0.jpg[0]' 2>/dev/null
        [paperclip] convert '/var/folders/QK/QK+tCPT-Gx8yGDxVQbDuF++++TI/-Tmp-/stream20110113-19714-1ucz75p-0.jpg[0]' -resize "25x25>" '/var/folders/QK/QK+tCPT-Gx8yGDxVQbDuF++++TI/-Tmp-/stream20110113-19714-1ucz75p-020110113-19714-r839l2-0' 2>/dev/null

but the image is gone on the new page....is there a way to preserve this image so anyone using the page doesnt have to browse for the image again

本文通过了以下过程: http: //ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html就我个人而言,我还没有尝试过。

A better idea might be to add client-side validations to the form, so that if the user enters invalid data, they are immediately prompted to correct it. If you're only supporting IE10+, you can use HTML5 validations, otherwise you'll have to use Javascript validations.

Basically, no. This is browser behaviour that the server has no control over. If you're worried about validations elsewhere in your model causing the user to have to reselect and re-upload their images then I'd suggest making the file attachment a separate step.

The browser resets the form when they come back to it. You might be able to simulate something that looks like a single step by uploading the file using ajax in the background, and having javascripty goodness keep the form from submitting until the file is uploaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM