Path to private assets on Ruby on Rails - ruby-on-rails

I have a PEM file that should be stored somewhere in my Ruby on Rails web app, and be referenced from the production.rb config file with the path, as per below.
APNS.pem = '/path/to/pem/file'
I want to keep this file private, so storing it in the assets folder is not a good idea. Where can I safely store it and what would be the path like?

You can place it anywhere in your app outside of public and assets and it will be inaccessable from the web. Placing it in config/ is a common option.
To build a path to the file, you would use Rails.root.join('config/file.pem')

Related

Ruby on Rails - setting specific path to directory in rails app

Hello I am attempting to implement Trumbowyg emojis plugin in my ruby on rails application. So I have referred to the below link for guidance on how to implement the plugin.
https://github.com/Alex-D/Trumbowyg/blob/master/examples/plugins/emoji.html
$('#editor').trumbowyg({
btns: [['emoji']]
});
function initEmoji() {
emojify.setConfig({
img_dir : '../../bower_components/emojify.js/dist/images/basic/',
});
emojify.run();
}
$('.trumbowyg-editor').bind('input propertychange', function() {
initEmoji();
});
initEmoji();
How do I store the images in a directory and make reference to the directory under img_dir (as shown above) in a rails app?
Option 1: Embedding into Asset Pipeline
Without knowing exactly how this Javascript library works, the image path needs to be added to the Asset Pipeline. Otherwise rake assets:precompile will not include all of the emoji images in the manifest file it generates and will not know how to serve those assets in the production environment (assuming default Production environment configuration).
See #config/initializers/assets.rb
# Add additional assets to the asset load path
Rails.application.config.assets.paths << Rails.root.join('path', 'to', 'emoji', 'image', 'directory')
Option 2: Serve static images from /public
Another option is to not serve these files from the Asset Pipeline at all. It may be too much trouble to coerce the Asset Pipeline into serving these assets correctly without modifying the Javascript library to use asset-url in all the JS and CSS files.
Instead, copy the files to a directory under /public so the Rails app can serve them. Then adjust the img_dir in your Javascript configuration to be the path where the files are in /public. Ex: /emoji_images if you put the files in /public/emoji_images.
Depending on how you're hosting the app, you may have to configure serve_static_files in config/environments/production.rb.

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.

Can't access files in Public folder

I have put three files in my public folder. One is a HTML file. I can access this through https://domain.com/file.html
When I try to access either of the other two files through the same method (a .ipa and a .plist) it tells me the file is not found - but I definitely uploaded them. I'm sure this is a beginners question but I'm struggling to find an answer. A link in the HTML needs to be able to access one of them and a link in that file (the plist) will direct to the ipa.
I have tried creating routes to the files but that didn't work.
serve_static_assets worked for me. I was running my Rails application in a Docker Container with Puma. No Apache, nor Nginx to serve static assets.
If you have default row in config (actual for Rails 6):
# config/environments/production.rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
You can append fix it to config.public_file_server.enabled = true.
Or append this variable to start command or docker config file:
RAILS_SERVE_STATIC_FILES=1

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

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

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