Ruby on rails: variable number of input boxes - ruby-on-rails

I'm working on an application where the user needs to be able to upload multiple files at once in a submission form. Ideally I'd start with, say, three file upload fields with an 'add additional files' button that dynamically adds input fields. I can do the Javascript to handle this of course, but how would I access this "array" of files within my controller?
I've looked at a few canned solutions, but they're much more flashy than what I need, and I don't need ajax uploading either - just a regular old submit is fine.
So, to summarize: what's the best way to access zero or many uploaded files in a rails application?
Thanks in advance.

Nevermind, I found what I was looking for via the 'nested_form' ruby gem.

Related

Is there a gem for ruby on rails that let's a user upload an image for a model?

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.

How to implement a rich text document store in Rails

I need a tiny (less than 100 total) document store in a Rails 3.2 app where a user can create, edit, save, and delete rich text documents (ideally including images and other attached items). There is no file system available. Everthing must go in PostgreSQL database tables. The documents will serve as system-wide email and message banner templates.
My approach has been CKEditor. But the ckeditor gem seems wired for attachments as files. Also, it does not provide create/update storage of the document itself, just attachments.
I know how to roll my own model/controller/view the CKEditor but surely there's a simpler way.
So what is the most direct way to my goal of the rich text document store? Is there a plugin or gem?
More CKEditor references: This thread makes it look like a major project, but it's 2 years old. This one makes it sound like the default, but no other document does.
More
Using S3, Dropbox as suggested are also nogo. As I said, data must reside in PgSQL tables. Good news: found the paperclip_database gem. Bad news: doesn't work seamlessly with the ckeditor gem. The main issue boils out to this bit in the source file database.rb:
def setup_paperclip_files_model
#TODO: This fails when your model is in a namespace.
Indeed it does, as in the Ckeditor::Asset model! Trying a monkey patch now. If anyone has already made these three gems work together correctly, I'll give you the bounty for a pointer!
Pretty sure CKEditor makes it fairly simple to edit database form fields, which is all you'd need for editing the document itself, correct?
As far as attachment storage, you can use paperclip to manage the attachments -- file storage is just the default. If you use paperclip, you can then use one of many storage options from there, such as Amazon S3 storage, Dropbox, or create your own.
Hope that helps.
The first thread you linked to has it correctly: you need to (find or) write a custom server connector, and configure CKEditor to use it. You can find the relevant updated docs here:
http://docs.ckeditor.com/#!/guide/dev_file_browser_api

How to creating an in-place editable form for embedded documents using mongoid in Rails3?

I have two models User(main document) and Contact(embedded document in User). I'm showing a list of contacts when displaying a user. I was wondering if there is a suggested way of creating an in place editable form for all the contacts that are shown on the same page.
Really appreciate an sort of examples.
You're going to have to employ a bit of Javascript to hook into the view to update the object via AJAX. There are ways to do this using ember-rails right out of the box, but you can pick any framework you like. Essentially, you'll need to be able to do a RESTful update of the user, making use of the remote: true parameter on your Rails form.

Add a file to a database in a Ruby on Rails application?

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.

How can I let my users upload multiple pictures at the same time?

I have a ruby on rails website. One feature allows people to upload pictures but users find it painstaking to upload pictures one at a time. What is the best way to allows users to upload multiple pictures at the same time?
try Uploadify or SWF upload. I've used both on Codeigniter projects but I prefer Uploadify. Both are flash based, have similar interfaces and are easily configurable. They allow multiple uploads in a queue and users can cancel the uploads too.
hope this helps
Multiple file fields or some javascript magic to do multiple file fields
I'm not a big fan of those javascript/flash uploaders, but they might be to your liking.
It's not problem to have two fields in your form for two different file attributes on an object. if you are using the paperclip plugin, you would just have two attachments in the model.
You may have a one-many situation where you want to allow the user to click "add file" or "remove file" in a dynamic way to expose more fields. If so, check out Ryan Bates site at railscasts.com - the last few episodes are about hierarchical forms.

Resources