I am working on a form with an attachment, using Paperclip.
If the model is invalid when the form is submitted then I want to save a temporary copy of the file, so the user doesn't have to re-upload the file after they fix the form submission.
What I'm doing now is to use regular Ruby file operations to save the file.. but this is pretty low level compared to using Paperclip.
What might work best to allow me to save a temp Paperclip attachment that will then allow me to move the temp file to my final object upon successful validation?
I'm thinking about a simple ActiveRecord object (ie., TempAttachment) where I can assign the uploaded file and then move it to the final object when the object saves successfully.
Does that make sense? Anyone have any thoughts?
You can certainly do this as you proposed. But while you're saving a temporary attachment object to keep track of this file... why not make your life a bit easier and just save the model itself flagged as 'incomplete'?
You can set an incomplete model to bypass many of your validations, while blocking out incomplete models from areas that should not be using them.
However, guaranteeing that you don't mix an 'incomplete' model with valid ones may get a bit complicated. Depending on the complexity of your application, this may not be a great idea - in which case I would recommend sticking with your initial plan.
Related
I am working on a little app and I am curious if there is a way for a user to use the generated scaffolding to both (1) assign an image file name to a field, and (2) to automatically upload that file to the server when successful.
The idea is to create a simpler editorial process. My understanding is that including a blob in the table would not be good coding standards, so... if I can't store it in the database directly, then we need to store the image file name. But at the same time we need to ensure that that image is truly available on the server. In which case I'd like to cover both cases at the same time - allowing the editor to do their job.
is there an existing gem that I can leverage? Thanks
I'm not quite sure about your use case, and it seems like that's important, something about adding an image in a text editing field?
To answer one question, no, you can't do it with the default Rails scoffolding, although RailsAdmin and ActiveAdmin have paperclip support (and yes, to second #emaillenin, paperclip is probably the most common option).
Also check out JQuery File Upload for my favorite "scaffolding"-esque file uploader; although it's more work than say RailsAdmin, it's a great experience because you get thumbnail previews while the upload is going and progress bars all for free.
If it helps, I have a photo gallery project stored on Github, not "open source" in the sense that I attempted to make it useful to others, but I don't care if people browse it. It uses jquery-file-upload. Useful files:
Gemfile has:
gem 'paperclip'
gem 'jquery-fileupload-rails'
Then the photo model, categories controller for uploading photos as a nested resource, and the file-upload template translated to HAML.
How about Paperclip gem? - It lets you upload images, assign filename to a column in your model, validations on your attachments and helper functions to display them in your views.
Paperclip is intended as an easy file attachment library for Active
Record. The intent behind it was to keep setup as easy as possible and
to treat files as much like other attributes as possible. This means
they aren't saved to their final locations on disk, nor are they
deleted if set to nil, until ActiveRecord::Base#save is called. It
manages validations based on size and presence, if required.
Some alternatives - CarrierWave and more.
So I know STI is the most reviled thing ever but I have an instance where I think it might actually make sense. My app is parsing a bunch of different types of xml files. Every file model stores the exact same information. Just some info about what user it is associated with, when it was uploaded, and where it is stored on S3.
After the xml file gets stored then I parse it for information which I use to create various other models. Each type of file is going to create different things. It is possible there could be 100 or more different types of xml files although I don't think I'm going to write parsers for that many. Does STI make sense in this case?
The downside I guess is models are all in one directory so it is going to flood that directory unless hack Rails and stick it in a subdir in models dir.
The other option is I have a kind field and put something in the lib directory that handles all this. Or I'm using resque, maybe every xml file parser should be it's own job. There are drawbacks to that though like it being kind of awkward to force a job in the rails console.
From your explanation, the 'file' model is only storing the results of the file upload process and associated meta data. Without more information about the other kinds of models being generated from the parsed XML data, I don't see why single table inheritance applies to this use case.
I'm trying to modify an existing Ruby on Rails project. I understand that forms and models are closely related. I'm trying to understand how to modify a form so that instead of accepting an upload, it stores a timestamp instead. So, my understanding is that I need to modify the view and the model. Is there anything else I need to modify? What happens to existing data that I have stored in ActiveRecord?
Unless you remove columns from the table with a new migration - data will be safe. It's a good practice to write tests, so when such situations occur that you need to modify something - you can test that everything still works.
Btw, I don't understand the logic that you are trying to implement. The form was uploading some file before, and you need to change that and remove the file upload and modify some timestamp in the record?
Your existing data is supposed to be fixed up with a migration, which you will need to write.
Given a Rails Form Post with X number of files.
How do you store the file in the Database?
Then, later, how do you then read the file?
Thanks.
A little more background. The file form post is from SendGrid's parse api. Then later I want to be able to read the file with delayed_job and then use paperclip to store the file and process it on S3.
Thanks
Pretty sure I just answered your other question the same way: https://github.com/jstorimer/delayed_paperclip does what you seem to want it to do.
Based on the limited information I know about your system, I'm assuming you've got some model that tracks the emails. That's a good start.
First I would like to clarify that in no situation should you be storing files in the database. Files go in the filesystem, where they can be read and written much faster.
Now with that in mind, I would have a model that associates with your email model, possibly called Part. This model's purpose will be to use Paperclip to store the files. I would call the attachment part also, and so to create a new one you would do this:
email.parts.build(:part => some_file)
In that case, Paperclip will take care of moving the file to where it needs to be. To read the file later, Paperclip has methods for that. Check out Paperclip's documentation, it's pretty good for this sort of thing.
I am currently setting up Paperclip for a model with Rails 3. When one of the fields fails validation (title), the user has to upload the file again. This is not very user friendly :/
The recommendation from the Paperclip forum is to move the Paperclip stuff into a related model. My model is very simple with just a few fields, so I would like to avoid having two pages/steps for creating a record.
arts/create (when valid) -> arts_image/create
Any suggestions?
I use the two-step solution with a separate model. Although it's possible to code and hack your way around the default behaviour, you could also validate on the client-side with JS.
Look at this article http://ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html
Cached version of the article: http://web.archive.org/web/20100919151143/http://ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html
I have taken a different approach by 'serving' the file back to the client and re-accepting it when the form gets resubmitted.
https://stackoverflow.com/a/25853569/7693