Upload file in Ruby on Rails: how to choose the folder - ruby-on-rails

I wrote a function in Ruby on Rails for uploading a PDF.
The file is uploaded to a folder in public, but every time I deploy to staging or production, the folder on the server seems to become empty.
How to choose the right folder?

In my opinion the easiest way will be to use paperclip
https://github.com/thoughtbot/paperclip
You can easily set folder somewhere like /home/rails/attachments
In documentation for paperclip you have
The files that are assigned as attachments are, by default, placed in the directory specified by the :path option to has_attached_file. By default, this location is :rails_root/public/system/:class/:attachment/:id_partition/:style/:filename. This location was chosen because on standard Capistrano deployments, the public/system directory is symlinked to the app's shared directory, meaning it will survive between deployments. For example, using that :path, you may have a file at /data/myapp/releases/20081229172410/public/system/users/avatar/000/000/013/small/my_pic.png

Related

Not all active storage files being loaded. Ruby on Rails. Production Active Storage

I have a Ruby on Rails application that had previous Active storage files already. Upon an update to the application, files that are not images are no longer able to be loaded (pdfs, dosx, etc.)
The server was setup with a local storage.
I am not for sure if this was caused by the update or if something happened to the files.
As you told in comments the problem is that between releases mina doesn't copy your saved ActiveStorage files
To fix that you need to set up shared folder
Add to your config/deploy.rb
set :shared_dirs, fetch(:shared_dirs, []).push('storage')
It could contain other shared folders already, just add ActiveStorage folder (by default it is storage, but you can check it in config/storage.yml)
Then copy from release folder to shared folder storage folder with files
Next deploy must be successful
Please look directory structure
https://github.com/mina-deploy/mina/blob/v0.3.8/Readme.md#directory-structure

Folder contents are automatically deleted every time I deploy my Rails app

I have a directory under /public folder with the use of CarrierWave I store all my PDF files under this dir. But the problem is all the time I deploy new version of my Rails app this directory gets cleaned up and the all files are missing. This directory is was set under my uploader.
I also have a directory named "private" which I created manually in order to not to serve sensitive files globally on WEB. Those files also gone after new deployment process.
How can I prevent files from deleting on deploy process?
Thanks.
I assume you are using some automation for deployment. Because if you are updating your code on server instance manually then you can preserve pre uploaded file, but using manually method to update code is not a good practice.
So in automation deployment we generally follow this kind of flow.
Whenever you deploy that create a new deploy version and set that as current version.
Simply that means it's creating a new directory and placing your rails project in it. Now the files you are storing inside the project directory are there in the previous version those are not gone if you are using any linux instance.(Only if you have setup that way to preserve last few versions to restore incase of new deploy is exploded)
Clear till now?
Not suppose you are not keeping any previous version, your files are gone forever.
So it's not a good idea to store any file under project repository.
Best practice is to use bucket system like AWS bucket or Google cloud bucket, where we store all the uploaded file. If having bucket is not in budget, you can choose a different directory on linux server instance outside of project directory. But you have to setup all those upload paths and directory system to be used as bucket.
This problem I am facing with is happening because of capistrano. Every time I run cap production deploy command on my server, the capistrano deployment tool syncs every file with git repo. And the files added by end-users are not stored under my git repos of course, so capistrano overwriting the empty public folder from my repo to the server. Adding the path to :linked_dirs variable under deploy.rb solved my problem.
Another approach could be using a directory which is somewhere else than your project root path (such as /home/files) to store all your files. By doing this you will be seperating your files from project and also prevent capistrano's overwriting problem.
Hope this information will be useful for someone or future me :) ..
When you deploy with capistrano, a new deploy(folder) is created from the repository.
Any files not in the repository are not carried over.
If you want to persist files in public, you need to create a directory in your server first and then create a symlink with capistrano inside public to that folder.
Then have your carrierwave uploads saved to that location.
During each deployment cap will symlink to that directory and your files will be there

Public assets with Capistrano and Rails not being uploaded

I have several sounds files that are located in public/assets/sounds.
Locally everything works fine, but when I deploy via Capistrano to my ec2 instance, none of those assets make it to the server. I added 'public/assets/sounds' to :linked_dirs in deploy.rb. A directory shows up at 'public/assets/sounds' but none of the mp3s are there. Do I need to manually add all files via :linked_files?
I have it working by just loading the files into the shared/public/assets/sounds directory via ftp, but that doesn't seem like the best use of the Capistrano. I'm also new to Capistrano and could be totally wrong :p
The public/assets directory is reserved for the Rails asset pipeline. You should not place any files there. Here's what I would do:
Remove public/assets/sounds from :linked_dirs.
Choose a different place for the mp3 files, like public/sounds.
Do not add this directory to :linked_dirs.

What is the purpose of the "system" folder in rails "public/system/"?

The standard place to store non-precompiled public assets in a rails app is in "public/system." Is there any reason for this? I'd like to keep things simple, why shouldn't I just put assets in the "public" folder?
The public/system folder isn't a Rails standard per se or even a documented recommendation. Deployment tools like Capistrano adopted that convention as a suggestion for you to organize user assets that are shared between deployments and shouldn't be in your repository. The idea is symlink public/system to capistrano's shared/system, which is outside of the releases folder.
If you take a look at paperclip's(which is a widely used upload library) docs you'll find:
By default, this location is
:rails_root/public/system/:class/:attachment/:id_partition/:style/:filename. This location was chosen because, on standard Capistrano deployments,
the public/system directory can be symlinked to the app's shared
directory, meaning it survives between deployments.
In your development environment you're supposed to ignore that folder in .gitignore if you're willing to adopt that convention.

Where should uploaded files get stored in Rails 3.1?

When user uploads files. In Rails 3.0+, these would go into public/uploads. In 3.1, should uploaded files go to app/assets/uploads? Or still in public/uploads?
It's not really an issue in our environment, since we are using S3. Just trying to understand Rails 3.1's new directory structure.
What are your thoughts?
the public directory, capistrano recommends public/system/
don't get confused by the app/assets directory, it's usually for css/js/coffeescript files, think this is the biggest change from 3.0 to 3.1
Well, the answer is simple: your users will only have access to your /public directory.
There are just some tricks to get css and js but you'll have to stick with /public for the other stuff.
Generally, I put all stuff in /public/assets
adding on to apneadiving's answer:
if you use Carrierwave , the temporary files are in your system's /tmp directory and the uploaded files are in a subdirectory underneath $RAILS_ROOT/public , e.g. $RAILS_ROOT/public/uploads/YOUR-MODEL/...
In Rails 3.1 the 'assets' directory is meant for the JavaScript and CSS files so that sprockets can pick them up there and so that they are not accessible directly via the "public" directory...
see: assets/javascripts/application.js and assets/stylesheets/application.css files
see: http://railscasts.com/episodes/265-rails-3-1-overview
The app/assets directory is for CoffeeScript files (also not publicly accessible, so not a place to put uploads)
Putting uploaded files in the filesystem only works if you have one file server or a network mapped storage... I usually just put the files in the database itself.
But as vrsmn said, don't use assets for this, assets pipeline is for streamlining the css/js/application images.

Resources