Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachment is lost if any validations fail #72

Closed
dbackeus opened this issue Aug 4, 2009 · 13 comments
Labels
Bug

Comments

@dbackeus
Copy link

@dbackeus dbackeus commented Aug 4, 2009

To quote an old comment on a blog I found mentioning this problem:

"For instance we’ve the model Medium which has_attachment :file and validates_presence_of :comment. If we left out the comment but specified a :file (let’s say a big one… 200MB…) the validation will fail and the data is lost.
It’s pretty annoying if you have to upload the file again."

Since the normal behaviour of an AR object is to keep the given attributes after re-rendering the form view after a failed validation it can come as quite the surprise to users that they will have to re-upload files like this.

@simmerz

This comment has been minimized.

Copy link

@simmerz simmerz commented Aug 13, 2009

This is especially the case for a form where the user can upload multiple attachments. Upload Column allows this, and references the location of the temp file. This allows a hidden field to be added to the response form with the location data for the temp file. When the form is then submitted with valid data, the file is retrieved and placed in the right place.

@jyurek

This comment has been minimized.

Copy link
Contributor

@jyurek jyurek commented Aug 21, 2009

Yes, this is the normal behavior of AR, but it's not the normal behavior of file uploads in general. Specficially, in order for this to happen, we would need special file_field helpers to display the file if it exists in the "temp" area. Also, this might not even be possible on environments like Heroku that do not allow filesystem access. I do eventually want to add functionality like this, but I will not be doing so for a while. I will gladly review and consider patches that allow this functionality, however.

@davidchua

This comment has been minimized.

Copy link

@davidchua davidchua commented Mar 25, 2010

Does anyone know of any temporary workarounds to keep the attachment even after a failed validation?

@x37v

This comment has been minimized.

Copy link

@x37v x37v commented Dec 21, 2010

Is there an explanation for why this 'feature' is closed? Is there some code now that solves this problem?

@wmakley

This comment has been minimized.

Copy link

@wmakley wmakley commented Mar 21, 2012

I, too, would like to know what, if anything, has happened with this. It's a significant usability issue for my app.

@matiasa18

This comment has been minimized.

Copy link

@matiasa18 matiasa18 commented Jul 11, 2012

I dont get why this is closed, but I guess ill have to keep googling.

@matiasa18

This comment has been minimized.

Copy link

@matiasa18 matiasa18 commented Jul 11, 2012

Found this around the internet; http://stackoverflow.com/questions/5198602/not-losing-paperclip-attachment-when-model-cannot-be-saved-due-to-validation-err Also, some dude on the IRC mentioned CarrierWave, so I guess it will be a mather of migrating or writing ofuscated code..

@jyurek

This comment has been minimized.

Copy link
Contributor

@jyurek jyurek commented Jul 11, 2012

It was closed, for those wondering, because it's not on my list of priorities. However. I'm completely willing to entertain pull requests for this issue, if anyone would like to give it a shot.

@mszyndel

This comment has been minimized.

Copy link

@mszyndel mszyndel commented Sep 5, 2013

So this still is a problem after 4 years since creation. Still not on a priority list?

@jyurek

This comment has been minimized.

Copy link
Contributor

@jyurek jyurek commented Sep 9, 2013

Well, no. This is a pretty complicated problem involving temporary storage, view helpers, and exposure to a much larger subset of Rails API. Solving this in the general case would probably greatly increase the size of the codebase. The problem is (IMHO) more easily and more correctly avoided than solved. And if you do need to solve it, then your own solution will be better for your problems than one we can supply for everyone.

So, no, it's still not on a priority list. Though, as I said, if you'd like to take a crack at it, I'm more than happy to help, give advice, and review the PR.

@mariohmol

This comment has been minimized.

Copy link

@mariohmol mariohmol commented Jan 21, 2015

thats ok. For those who need this feature, I just started a repo to share some ideias with workarounds.

@abrambailey

This comment has been minimized.

Copy link

@abrambailey abrambailey commented Apr 4, 2015

The Stack overflow answer was to switch to carrierwave. Pity!

@nageswararao-nyros

This comment has been minimized.

Copy link

@nageswararao-nyros nageswararao-nyros commented Dec 20, 2018

model.rb

validates :title,:story, presence: { message: "Must be given please" }, length: { in:6..20 }
validates :author_id, presence: true
validates :poster, attachment_presence: true
validates :title, length:{in:6..20}

has_attached_file :poster, styles: {
thumb: '10x10>',
square: '200x200#',
medium: '50x50>',
pdf_preview: '600x600>'}, processors: [:thumbnail]
validates_attachment :poster, :content_type => {:content_type => %w(image/jpeg image/jpg image/png application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)},
attachment_size: { less_than: 5.megabytes }
has_attached_file :poster

belongs_to :author, required: false
has_and_belongs_to_many :reviews

after_create :article_publishing
after_update :article_update

These are my Rails model validations, If i submit empty form data not saved in database.Here is the problem errors are also not showing,why??

This my code for Views errors for one field.......
index.html.erb

<%= form.label @article.errors.messages[:story].first if @article.errors.messages[:story].any? %>

can any one give the answer, How to show the errors in View, How to rectify mo problem........Thankyou all

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.