I need to create a model application form where models can fill it and add pictures to it.
I'm following this example with Uploadify, Paperclip and Rails 3 approach.
https://github.com/websymphony/Rails3-Paperclip-Uploadify
To the pictures, i have an polymorphic Attachment model and i would like to attach those ajax uploaded attachments to the yet unsaved model form and there's is where the tricky part cames.
Users are not logged, so there's no "model_id" until is saved.
Since i'm showing the user a small preview of the images that he uploaded in the form by ajax after each upload i need some way to correlate them.
I was thinking about some middle token until the model is saved but i'm not sure whats the best approach to accomplish this.
Thank you!
We dumped Uploadify when moving to Rails 3 and are now using jQuery-File-Upload.
https://github.com/blueimp/jQuery-File-Upload
Setting up a bunch of middleware to do nothing more than upload files seems like a major hassle.
Only problem with the jquery solution is multiple uploads aren't supported in IE.
Related
I'm using Ruby On Rails 4 + ActiveRecord + CarrierWave to store images.
Now I have task - to store many image frames in each AR record for SpriteSpin 360 views. I have trouble with traditional way to store images - SpriteSpin requires up to 34 image frames. I think it isn't best way to create 34 or more attributes, and upload each frame separately.
Maybe there is more correct way to upload and store it?
I think CarrierWave doesn't support multiple uploads. It's designed to associate a single file with a single field.
If you want multiple uploads, you need either multiple fields (each with a CarrierWave uploader), or multiple objects each with a single CarrierWave uploader field.
The multiple attribute is also unsupported, so if you use it, it's entirely up to you to get the parameters assigned properly.
Here are some references of multiple uploads via Carrierwave. I am not sure you like these tricks or not? But for your reference I found..
Multiple file upload with carrierwave, nested form and jquery file upload
Multiple file uploads with Ajax, Carrierwave, & Mongoid
Stack Overflow's Answer
I hope this helps you somehow... Or wait for someone's answer who had done this before. I am big fan of and love to use Paperclip
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.
I want users to be able to upload data files that will be processed in the background on my application.
I see many examples of using paperclip to allow files to be attached to specific models. But these files don't have a one-to-one correspondence to any of my models. How should I model this in Rails 3?
The way I would approach it, barring any input from people smarter than me, is to define "file types" for a specific model that is associated with the user account model itself. Then the upload process would place those files in a specific directory where they would be picked up by a poller that would then process the files.
Polymorphic Paperclip?
http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/
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
I've only just started learning ruby on rails and I would like to create an application that will allow me to add files to the database. Currently, I'm developing the rails application using the Aptana plugin for Eclipse and the application is using the default sqllite db.
I have tried generating a scaffold with the following parameters: documents title:string file:varbinary. Then I do a 'rake'->'db'->'migrate'. When I migrate to localhost/documents and click on 'New Document' the application fails and displays an error.
What I would like to do is click on 'New Document', have a field that will allow me to browse for a document on my local computer, select the document and add it to the db on the rails application.
Paperclip is more recommended than attachment_fu these days. It is really simple and easy to use with your active record model.
Is it a particular kind of file you want to add?
I just ask because if it's not data of a kind that benefits from being in a database ( textual data might be searchable, binary data is not ) then you are much better storing it in the filesystem and serving it up straight - especially for stuff like images or video - rather than inserting it into a database and having to go through your application every time a user requests it.
I'm not saying that there aren't any reasons you might want to have a file in the database, but I treat that as a last resort and in ten years of web programming I've not come across a case where it was necessary.
I would highly recommend the attachment_fu plugin as this lets you create models with attachments pretty nicely, Paperclip plugin is another good one also!
If you have trouble deciding which one to use, as far as i can remember, Paperclip makes it easier for multiple attachments, such as an Album has many Photos, and Attachment_fu is easier for single attachments such as a User has one display picture.
We do something like this on a site I'm managing. Instead of storing these files in a database, I'd agree with the other posters here and recommend you try something like Paperclip.
One caveat: if you want access control, make sure that paperclip doesn't save your files somewhere under /public, where anyone could possibly access them if they knew the URL. Deliver files to the user via send_file in your controller.