开发者

Paperclip fails on a validates_presence_of

开发者 https://www.devze.com 2023-02-04 10:20 出处:网络
Ok so i have a model and in that model i have class User < ActiveRecord::Base validates_presence_of :name

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

class User < ActiveRecord::Base
  validates_presence_of :name
  has_att开发者_运维问答ached_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


This article goes through the process: http://ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html Personally I haven't tried it yet.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消