Routing error for images Ror - ruby-on-rails

I run my app locally on 3000 and all is fine, but when i upload to heroku it is sub standard.
I am using paperclip gem,
my Heroku logs issues this error
ActionController::RoutingError (No route matches [GET] "/images/medium/missing.png"):,
There seems to be no path to this instance in my pipeline, If it runs locally and not in heroku i figure it's a Heroku config problem?.

For heroku you have to additionally use the aws-sdk gem like shorlty described in the excellent paperclip-intro.
Sample configuration:
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Best info point is usually heroku itself, see this example configuration guide

paperclip gem means you are uploading images, and want to access the image via that GET request right?
that path looks like it is for the heroku server itself. Heroku doesn't support file uploads, get amazon s3 setup or some other storage facility.
it works locally because your local setup allows for file uploads and storage.

Related

Sitemap generator not uploading sitemap

I started playing with a test app trying to upload sitemaps using Amazon S3. I've been following https://github.com/kjvarga/sitemap_generator trying to figure out the gem and have only been half successful. A sitemap will generate in the public folder, but not upload to the S3 bucket.
I've added the config/sitemap.rb found in the tutorial above.
require 'rubygems'
require 'sitemap_generator'
require 'aws-sdk'
SitemapGenerator::Sitemap.create_index = true
SitemapGenerator::Sitemap.default_host = 'https://www.myapp.herokuapp.com'
SitemapGenerator::Sitemap.create do
add '/home', :changefreq => 'daily', :priority => 0.9
end
SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(fog_provider: 'AWS',
aws_access_key_id: 'KEY',
aws_secret_access_key: 'SECRET',
fog_directory: 'DIR',
fog_region: 'REGION')
I type in
rails sitemap:refresh
in my terminal and it generates the maps. It just doesn't upload them. No errors, no clues as to what didn't happen, nothing. It even tells me that google and bing are successfully pinged.
Of course I can visit my AWS bucket and manually upload these files but that feels...wrong. I've used shrine for images in the past and am used to uploading to a cache. There must be something I missed.
Check your secrets, maybe you don't have the aws account env vars so S3 adapter would never works or policy in your bucket

Paperclip S3 issue, no method match

Been trying to integrate S3 into my paperclip class.
Did so successfully on my local and a coworker's, but it's not working on production.
Here's my image class's paperclip code:
has_attached_file :image,
storage: :s3,
:path => ":imageable_type/:imageable_id/:filename",
s3_credentials: {
access_key_id: ENV["AWS_KEY"],
secret_access_key: ENV["AWS_SECRET"],
bucket: ENV["S3_BUCKET"]
},
s3_region: ENV["S3_REGION"]
I'm getting the following error NoMethodError (undefined method 'match' for nil:NilClass):. A quick search shows it to be related to the s3_region not being mentioned.
It works in my development server and they the s3_region is included in my configurations.
I have defined the interpolations and environment variables. The environment variables are working in the rails console so that's not an issue I guess.
Any help would be appreciated
what are you using to deploy to production?
For example, if you use Heroku, you need to mantain separately the env variables in your production server. You can do this via dashboard or the terminal
What are the other heroku environment variables?
https://devcenter.heroku.com/articles/config-vars
If you use AWS then read here
http://docs.aws.amazon.com/lambda/latest/dg/env_variables.html
For docker read here
https://docs.docker.com/compose/link-env-deprecated/
The env variables are defined in files like ~/.bash_profile or ~/.bashrc of every ubuntu server/container you are running
Read more about environment variable and how to configure them

AWS Permanent Redirect Error with Paperclip & Rails

Up until today, I have had no issues setting up S3 on my development environment. I am now constantly getting the following permanent redirect error:
AWS::S3::Errors::PermanentRedirect at /locations/1
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
Paperclip is working perfectly fine. The issue only arose when I tried to move hosting images onto S3.
1/ the AWS-SDK gems is installed
// gemfile
gem 'aws-sdk', '< 2.0'
2/ Paperclip defaults are properly setup; both access keys are configured correctly in my bash_profile
//development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => 'traipse-temp',
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
3/ I have a S3 bucket created titled "traipse-temp" set to US Standard
Does anyone have any insight as to why I am receiving a permanent redirect error? I have setup 4 other applications this exact same why but only with this latest project did I receive this first-time error.
Thanks in advance.

Upload image with RoR and Heroku

I recently deployed my ruby on rails app on Heroku and I am using the pg gem for my database.
I created a page where I can upload images and have them show on the main page and they are saved in a public folder within the app.
When I deployed it was working fine, but after sometime the images disappeared. When I looked at my Heroku logs I got the following:
ActionController::RoutingError (No route matches [GET] "/assets/products/6/photos/medium/shanab.jpg"):
I think the images are saved, but I don't know how to check if they are still there.
but after sometime the images disappeared
Heroku runs an ephemeral file system which means that each time you deploy a new version of your code, the old files/code are overwritten:
Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.
This means that even if your images are working for a time, each new deploy will basically overwrite them. This is probably the cause of the problem, although I can't be sure without further inspection.
--
The way around this is to use a CDN, most common (although incorrect) is to tie your app with S3.
I don't know which asset-processing gem you're using, but if it's Paperclip, you'll want to use the inbuilt S3 functionality to put all your image assets onto Amazon's servers:
#app/models/x.rb
class x < ActiveRecord::Base
has_attached_file :download,
:storage => :s3,
:s3_credentials => Proc.new{|a| a.instance.s3_credentials }
def s3_credentials
{:bucket => "xxx", :access_key_id => "xxx", :secret_access_key => "xxx"}
end
end

Deploying on Heroku - Images disappears after upload

I deployed an application on Heroku which I wrote on Ruby on Rails. It is a movie review app.
I am able to upload images from my computer, to the web app online. Everything else works as per my expectations.
The images disappear after a day. My requirement is to have the image continue to render.
I am using Paperclip gem from rails. This only happens on the deployed version and not on localhost.
The default upload location on Heroku is into temporary storage. This is because you will get a different web worker each time you deploy.
You need to use S3 or another location to store your files. Luckily this is well documented for Paperclip on Heroku.
The main configuration difference is this. Add gem 'aws-sdk' to your Gemfile and then adjust your config file in config/environments/production.rb:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}

Resources