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
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.
I have a rails 3 application that involves a lot of importing of CSV files from a third party into various rails models. I have developed some scripts to perform most of the heavy lifting and today I added a lot of validation to the models to make sure valid data is going into the models/tables.
I'd also like to do some logging of validation errors so I can stay on top of all of the data processing and catch and correct validation errors quickly. To that end, I have taken the following steps:
I created a ValidationError model to store validation errors.
I initially create an instance using new.
Once the object is built, I use valid? method and an if conditional to determine if the object it valid. If the the object is valid, I simply save it. If the object isn't valid I create a validation error record.
I use the errors method to populate my ValidationsError instance.
Then I create the ValidationError.create(validation_hash[])
I'll display the validationError model in my ActiveAdmin dashboard and send out an email when validation entries are created.
Question(s): Is my proposed approach to capturing validation error reasonable. Are there approaches that have been used by others that may be better/preferable to what I propose. Is anyone aware of any gems or built-in functionality that would accomplish what I'm trying to do.
Have a look at validation_rage gem. you might be able to use that gem to achieve what you want.
have also a look at this :)
Importing CSV file into multiple models at one time
and rail_admin_import gem, maybe you can expand with above staging table approach.
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.
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.
I have my form and validation working great with regular http requests. I would like it to use AJAX. I know i can validate on the client side but that seems redundant given that I have defined the validations in my model.
As the user is filling out the form, I'd like to give feedback to them on their entries. What is the best way to use the rails defined validations in an AJAX form and give live feedback?
Check out the live-validations plugin. There's also an introductory screencast.
For Rails 3 check out Client Side Validations: https://github.com/bcardarella/client_side_validations
Here's the railscast: http://railscasts.com/episodes/263-client-side-validations
Live-validations was kinda messy to get working for me, so I started with my own solution from scratch backed by Validatious. It's actually really DRY because of the Rails conventions in the back that made it possible to do a lot of smart assumptions. In most cases, all you need is to include a few javascript dependencies and declare your validations in your models as always - and it all just works! =) Peep the README for details.
Here it is:
http://github.com/grimen/validatious-on-rails
If you're looking for a solution to this that does not introduce any plugin dependencies check out my screencast on the issue:
AJAX Validations on Rails 2.3.8
https://github.com/augustl/live-validations/wiki has installation instructions.
When you add LiveValidations.use :jquery_validations to the bottom of your environment.rb, make sure it is outside of the Rails::Initializer block.