My Ruby on Rails application uses S3 and Paperclip. I have users upload a text file, which works fine. I also want to allow them to edit the text file and resave it… this is where I’m confused. Since they’re not re-uploading any file, but rather EDITING the contents of the saved text file. How can I do this?
Equivalently, how do you create / save a model with Paperclip, without uploading an actual file?
Here’s something I tried…
Source.new(:user_id => 4,
:name => "untitled.txt",
:attachment_file_name => "untitled.txt",
:attachment_content_type => "application/octet-stream",
:attachment_contents => "This is a sample text file. Edit and resave to change this.")
.save
PS – I’m saving the files on S3 instead of a database blog because they could potentially be large, and that seems unsuited for a database.
EDIT: I’m adding a bounty that I’ll award to anyone who can show me how to do this without requiring the user to save a text file and reupload it. I have a HTML textarea and want to offer an AJAXed “Save” button to resave a text file on S3.
Sounds like you are mixing two conceptually different approaches: DB store and file store. So from my point of view, you should go for either one of the 2 options:
Option 1: Use a file storage (like Amazon S3)
This seems to be answered thoroughly by Ben Simpson. If you want to allow the user to edit a file-backed resource, then you have to make sure to “manipulate the contents of the file on S3 outside of Paperclip.”
Option 2: Use db-storage
It sounds to me like this is what you ultimately want to achieve. The file upload serves as the entry point for user-created content into your application. Since you are dealing with simple text files, I suggest saving the uploaded file’s contents into your model, which you can then update just like any other model (including your ajax save).
For the initial values, i.e. “This is a sample text file. Edit and resave to change this.”, you can either use the :default option in the migration file, or if you insist on a physical file to be present, use paperclip :default_url on the has_attached_file method along with a file that includes the template content.