0

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

rockit
  • 3,708
  • 7
  • 26
  • 36
  • possible duplicate of [Need assistance choosing a image management gem](http://stackoverflow.com/questions/16447366/need-assistance-choosing-a-image-management-gem) – Uelb Mar 22 '14 at 09:30

2 Answers2

2

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.

Community
  • 1
  • 1
Raj
  • 22,346
  • 14
  • 99
  • 142
2

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.

Woahdae
  • 4,951
  • 2
  • 28
  • 26