FileNotFound after deploy - ruby-on-rails

I'm working with simple rails 6 application and use active storage with Amazon S3, it works fine. But after I deploy anything all of my pic URLs have 500 errors. But my attachments still in the database and all my pictures stay untouched on S3 and URLs to rails/active_storage/representations are the very same before deploy when all was fine.
All credentials are encrypted in credentials.yml.enc and master.key in the config folder and if rails can start and upload a file into s3 in this way it works fine?
Here how I display picture, nothing special:
image_tag #product.images.first.variant(resize: "420x")
I guess it's something with some secret tokens which augmentates application but couldn't find anything relevant. Can someone advise at least where can be a problem?

Related

How to use File.new() in Rails app to create a file?

I would like to create some_javascript_file.js after a user submits a form and save this file in the public directory of my app.
When testing this localy I can simply use File.new("./public/some_javascript_file.js", "w") to accomplish this but this solution doesn't work when the app is deployed to Heroku.
Please would you have any suggestions? Thank you
This is because of how Heroku works. Basically, as stated in the docs, the filesystem is ephemeral and you can't rely on it. If you want persistence, you should upload this JS file to some external service, such as AWS S3. Or you might want to deploy your app to a different environment, such as self-managed VPS, where filesystem is real and your file won't disappear.

Configuring a Heroku Rails app for CKEditor to work on Cloudfront

I'm using this gem to integrate CKEditor into my Rails app, with paperclip to handle image uploading.
I no longer want Heroku to serve my CKEditor-uploaded images anymore, so I'm switching to Cloudfront and found this tutorial.
As I understood (from the tutorial), I can use Cloudfront without using an S3 bucket, as Cloudfront will automatically fetch my precompiled static assets stored on Heroku. But I'm so confused:
Will image uploaded via CKEditor be included in the asset pipeline? So that when precompiled, will be served by Cloudfront?
The tutorial said I need to change all image links to <%= image_tag('...') %> so that it'll point to Cloudfront and work. But this is only possible when I hard-coded the image, not the case when a user uploaded one in his text and stored in the database. Am I wrong and how to solve it?
Will this method (not using S3 bucket) work for other "dynamic" images such as User's avatars, post's cover images, etc...?
I don't want to use S3 bucket as it will involve the use of asset_sync. Any help is appreciated, thanks!

Rails] Images erased after a new commit on heroku

I'm using Carrierwave to upload images, and my app is on Heroku right now.
Images are uploaded successfully unless I push a new commit to heroku. Images that I uploaded before a push seem to be erased when a new commit comes in. Does anyone know the reason behind and how to fix this issue?
Update:
The problem becomes, using carrierwave on Heroku without a storage server like Amazon EC3. Heroku does not save files in public folder, where carrierwave uploads by default.
app/uploaders/image_uploader.rb:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
And you need to change this to somewhere in the tmp folder in order to preserve your image files after each commit. I tried to change it to
"#{Rails.root}/tmp/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
But it does not work. I checked on my local environment, and the image is stored in the right location successfully. So I guess if I just set the routes to the right location, this could work. Could someone help me with this?
Since Heroku does not allow storing static files (unless it's associated the application itself), you should make Carrierwave upload to a remote repository (e.g. Amazon S3) using fog. Everytime a user uploads a file, the file will be automatically uploaded to your S3 storage instead of Heroku.

Paperclip: migrating from file system storage to Amazon S3

I have a RoR website, where users can upload photos. I use paperclip gem to upload the photos and store them on the server as files. I am planning to move to Amazon S3 for storing the photos. I need to move all my existing photos from server to Amazon S3. Can someone tell me the best way for moving the photos. Thanks !
You'll want to log into your AWS Console and create a bucket structure to facilitate your images. Neither S3 nor Paperclip have any tools in the way of bulk migrations from file system -> s3, you'll need to use the tool s3cmd for that. In particular, you're interested in the s3cmd sync command, something along the lines of:
s3cmd sync ./public/system/images/ s3://imagesbucket
If you have any image urls hard-coded into your database (a la markdown/template code) this might be a little tricky. One option would be to manually update your urls to point to the new bucket. Alternatively, you can rack-rewrite.
You can easily do this by creating a bucket on Amazon S3 that has the same folder structure as your public directory on your Rails app.
So say for instance, you create a new bucket on Amazon S3 called MyBucket and it has a folder in it called images. You'd just move all of your images within your Rails app's images folder over to that new bucket's images folder.
Then you can set up your app to use an asset host like this answer describes: is it good to use S3 for Rails "public/images" and there an easy way to do it?
If you are using image_tag or other tag helpers (javascripts, stylesheets, etc), then it will use that asset_host for production environments and properly generate the URL to your S3 bucket.
I found this script which takes care of moving the images to Amazon S3 bucket using rake task.
https://gist.github.com/924617

Upload File to AWS S3 with Rails

I have an external application uploading a file to my rails web app. (Developed on Heroku, but that's not completely relevant)
I need to upload that file to AWS. I've successfully connected to AWS with rails, but I can't figure out how to access the file that was uploaded.
I know I need to do File.open({path to file}), then pass the File to AWS
I forgot to set the multipart enctype in my test external application...if anyone else has this same problem this code should work:
AWS::S3::S3Object.store('{filename.ext_for_aws}',params[:{name_from_post}],'{bucket}')

Resources