Cloudfront on rails not showing new images - ruby-on-rails

I'm using cloudfront as my CDN in my Rails app. I created my distribution and changed the enviroment file to enable the asset host.
Everything was working fine until I made a new deploy that included 3 new images. After restarting, everything looks fine but the 3 images. If I get the cloudfront URL and change the domain for my rails app domain the images load just fine, but if I use the CloudFront domain the images look like they weren't found.
Any ideas why this is happening? If I undestand correctly CloudFront doesn't have a delay, it loads the image as soon as the first request comes in.

run rake assets:precompile then upload it.
or check the image path
it should begin with "/" like
<img src="/images/img.jpg"/> if you are images in public assets

I found the problem. Sometime between the precompile and the restart of the server someone made a request and Cloudfront couldn't find the image so that's why it wasn't showing. I changed the image name, re-depoyed it and everything is fine now.

Related

Cloudinary upload image with HTTPS url

I am using a Rails app to upload images (or files) to cloudinary. I am using Carrierwave as uploader. Everything works fine locally, but I see that images are uploaded with http://something.
This does not work in production, since my domain is HTTPS, therefore, when I ask for a HTTP, it fails, since I need HTTPS.
Any help? I checked the documentation and they say that they work with HTTPS, that's why I am confused. I also wrote them, but it would be nice if someone had the same issue and knows how to solve it.
The upload response includes "secure_url", or you can just use "https://res.cloudinary.com..."
--Yakir

Rerouting to AWS hosted assets from a Heroku Rails app

I have a Heroku hosted Rails app that has reached the 300MB limit for the slug size and can no push to Heroku. To fix this issue, I've setup an AWS S3 account and want to redirect the assets being requested from my Rails app to the new S3 location. The Rails app is basically just serving JSON files that point to the static assets using a relative URL. An iOS app using the Rails JSON then has a hardcoded domain URL, and appends the path to the resources to that domain and requests assets it needs.
I want to update my Heroku app and change my asset locations without requiring an update to the iOS app in order to change the asset domain. So, I need to redirect static asset requests from the Rails app to the AWS server.
In my git repo, I've ignored the assets in the public folder that I've moved to the AWS server. The asset files are present on my local machine, but are not part of the git repo when uploaded to Heroku.
So far I've tried changing the config.action_controller.asset_host which does not seem to work since these are static assets and are being returned by the web server before Rails gets it.
I tried using routes rules to redirect to a different domain, but these routes never seem to be captured. The static files appear to be returned before the Rails app has a chance to handle the request.
I've tried using the rack-rewrite gem to try and redirect my assets to a different domain with the following in `initializers/rack_rewrite.rb:
require 'rack/rewrite'
AppNamespace::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
r301 %r{/images(.*)}, 'http://my-subdomain.amazonaws.com/images$1'
end
This doesn't seem to work either and it always just returns the static files.
So far I've been trying for hours to figure out the proper way to handle this situation. I'm not a Rails developer by trade, I build iOS apps, so please let me know if I'm going about this the wrong way or completely missed the "right" way of doing this.
I solved this using routes and the 'rails_serve_static_assets'. I had to move the local images out of the public folder in order to avoid Nginx from returning the images before hitting the Rails app. The route ended up like so:
match '/images/(*path)', :to => redirect { |params, request|
"#{Rails.configuration.assets.aws_url}#{request.path}"
}

Problems serving .js and .css assets cloudfront CDN Heroku Rails 4 App

I am trying to host the assets of my Rails 4 App over Cloudfront CDN. I used to use asset_sync and s3 but I wanted to switch over to a CDN.
When I go to my Heroku App, I see that all the pages are just bare HTML. None of the JS or CSS is being loaded.
These are the errors I am getting from my Console:
Screenshot of console errors: http://i61.tinypic.com/15rxdsj.jpg
Also, I am not sure if I have set up my Origin Domain Name and Origin Path correctly on the Cloudfront Origin Settings.
Currently I am using my heroku app url as the Origin Domain Name and "/production/assets" as the Origin Path.
Production.rb file: http://pastebin.com/2dzLpGfE
I have been trying unsuccessfully for the past week to get the Heroku app to display the CSS and JS. I would greatly appreciate any insight. Thanks in advance!
Moving to Cloudfront isn't as drastic as change as you seem to think if you had everything working from S3. After all, Cloudfront simply distributes the contents of your S3 bucket to edge locations. This means you just have to let Rails know to look for the CDN and not S3.
There are a lot of things that could be going on. You could have misconfigured Cloudfront, which should be pointing to your S3 bucket as the origin. You should test that setup by checking to see an asset in the browser by using the Cloudfront URL. The main point is that Cloudfront should have no idea about your Rails app.
Meanwhile, you can still use AssetSync to push your assets to S3 "underneath" an assets path. You must also configure config.action_controller.asset_host as described here.

Uploading image into assets folder

I was following this guide for uploading images with PaperClip over ActiveAdmin:
File upload with Activeadmin Rails using paperclip
Everything is working fine, but I was wondering, after I upload image, how to redirect it to be uploaded into /assets/images/ instead only in database?
You don't.
It won't work anyway. assets/images is a source directory for rails asset pipeline. On your production server, the images are not used from there but from another folder where they have been updated by the pipeline (typically getting a name with a linked timestamp, so that the browsers can cache them properly).
To make this work you would need to upload them to the assets/images, then run again the asset pipeline, which would be... weird.
If you don't want to store them in the public folder, look at an external storage like S3 (sample using Heroku: https://devcenter.heroku.com/articles/paperclip-s3).

Rails 3.1 image asset timeout

I'm having intermittent issues with images on Rails 3.1.
The file in question is a simple PNG; all other images are working correctly. If I vist the asset path directly the image works as expected. Restarting the server (Webrick, in development) does not help. Clearing the browser cache does not help.
When I reload the page the image shows but the browser indicates it is still loading. After 30 seconds the request times out and the image is replaced with a broken image indicator.
Here are a few supporting images:
https://skitch.com/matt_darby/fahke/oops-google-chrome-could-not-connect-to-localhost-3000
https://skitch.com/matt_darby/fahkp/oops-google-chrome-could-not-connect-to-localhost-3000
https://skitch.com/matt_darby/fahkw/contact.html.haml-documents-code-protectedmethod.com-app-views-pages-vim
I too had this issue and Richard Hulse's comment above to his own answer: "delete the files in tmp/cache/assets and restart' fixed it for me. Thanks Richard.
I suspect this is not working because you are running in production mode locally, or have digest turned on in your config.
In your view you should be using the asset_tag helper:
%img{ :src => asset_path('/assets/phone.png') }
This ensures that the correct path (with the fingerprint) is rendered into the view.
The code you have will work in development mode (or without digests), but will break when they are on, or in production.
If this is an upgraded app, check the Rails asset pipeline guide for the correct stuff to paste into your config files.

Resources