Matt Elhotiby
Matt Elhotiby

Reputation: 44066

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

Upvotes: 0

Views: 529

Answers (3)

Fiona T
Fiona T

Reputation: 1929

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.

Upvotes: 0

BeepDog
BeepDog

Reputation: 5075

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.

Upvotes: 0

noodl
noodl

Reputation: 17388

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.

Upvotes: 0

Related Questions