How to implement a rich text document store in Rails - ruby-on-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

Related

Organise Active Storage files for use in other systems?

Active Storage stores uploads in a file structure like so:
This is great if the rails app is the only software that needs to use these files.
But what if the app simply serves as a means to upload the images to S3 so that some other (completely separate) service can consume them?
The problem being, other developers wouldn't be able to make any sense of the directory and files, since they're labelled in a way rails can understand, but which a human cannot (e.g. what does folder named "O2" mean?).
Is there some way to ensure uploads are stored in a human-friendly way? e.g. each folder could be a user_id, with their assets inside? (or similar)
As far as I remember, you would have to implement an own service that somehow replace the key used to something else (e.g. S3Service), or patch ActiveStorage to create the key itself in a different way. I am not sure that this would suffice though, as the key is most likely used for other critical functionality outside of the Service.
Alternatively you might poke in ActiveStorage::Blob to fiddle with the key. That said, ActiveStorage does not support what you'd like out of the box and you would have to take the risk of messing with its internals.
A solution to the problem (but not answer to you question) might be implementing an API as outlined in the comments to your question.

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.

What is the simplest Rails file upload method?

I've looked at the available options and it seems like everything is optimized for image uploading as display. I just need simple file upload and retrieval. Are there any good options?
Paperclip is a popular choice for uploading and sizing images, but you can upload any type of file with it (doc, zip, txt, pdf... anything). Highly recommended. https://github.com/thoughtbot/paperclip
I like carrierwave. It has built in support for s3, has no workaround for setting up apps on heeroku unlike paperclip.
I use Carrierwave for mine and have been happy with it. I am just uploading general files, not specifically images. It is easy to implement and has good advanced features if you need them later. It also integrates with Fog to make using remote storage sources (like s3 or rackspace cloud files) easy.
Carrierwave benefits:
With carrierwave, the attachment is a seperate model instead of an attribute on an existing model, which might make things cleaner to work with.
It comes with the ability to attach a file via url (user passes in a url to a file) instead of uploading with a form).
It comes with some sort of way to remember files across form validation failures, although I've never used this and I'm not sure how it's done... maybe with two forms and ajax?
It seems to have a more engaged and enthusiastic community around it, with more projects extending it.
For S3, they use fog instead of aws-s3, and fog has much more active development.
That said, paperclip is pretty great and is actively maintained, and might come with handier default image manipulation stuff, I'm not sure.

Rails - Given a File Form Posted, How to Store in the DB and then Read later?

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.

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.

Resources