How to host assets locally under different hostname? - ruby-on-rails

I would like to test my assets from multiple hostnames ex: http://assets1.somehost.dev, http://assets2.somehost.dev
My assets configs from development.rb
config.serve_static_assets = false
config.assets.compile = true
config.assets.digest = true
config.action_controller.asset_host = "http://assets%d.somehost.com"
config.assets.compress = false
config.assets.debug = false
So I would like to setup config.action_controller.asset_host = "http://assets%d.somehost.dev"
To test that locally.

Add the following line to your hosts file (I think rails only uses four assets hosts by default):
127.0.0.1 http://assets1.somehost.dev http://assets2.somehost.dev http://assets3.somehost.dev http://assets4.somehost.dev
Wikipedia has a list of where to find your hosts file based on your platform.

Related

rails 4 not using digests in asset filenames, but only in production

I have a Rails 4 app that was recently heavily upgraded (in terms of gem versions and some other things). Deploys have been working fine, but once we cleared out our tmp directories we noticed that assets stopped working in production. What's happening is for some reason in production mode, the helpers aren't using digests in any of the asset filenames (e.g. /javascripts/application.js instead of /javascripts/application-some-digest.js). This causes those assets to 404, since they do exist with their proper digest names in the public directory and Google App Engine is set up to independently serve static files for the public directory (which has always worked fine). What is really strange, though, is that in staging mode, the app is doing everything properly, so there is something about our production environment that is making the helpers not use digests.
Even weirder, though, is if I go RAILS_ENV=production rails console and do helper.asset_path 'application.js' I get the proper filename with the digest. What on earth could be going on?
And yes, we are doing RAILS_ENV=production rake:assets:precompile before deploying.
Here are the relevant parts from config/environments/production.rb:
config.eager_load = true
config.assets.cache_store = :dalli_store
# Don't force SSL because we need non-ssl cookies.
config.force_ssl = false
config.stripe_livemode = true
config.assets.digest = true
config.assets.compile = false
Here are the relevant parts from config/environments/shared.rb:
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_files = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Precompile additional assets (application.js, application.css, and all
# non-JS/CSS are already added)
config.assets.precompile += ['zxcvbn.js', 'hammer.min.js', 'jquery.ba-throttle-debounce.min.js', 'mediaCheck.js', 'application-no-mq.css', 'lte-ie7.js', 'mailcheck.js', 'browserconfig.xml', 'main.css', 'oamm.js', 'oamm.min.js']
Here are the relevant parts from config/application.rb:
config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
# Enable the asset pipeline
config.assets.enabled = true
config.log_level = :info
config.assets.precompile += ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
And finally, here are the relevant parts from config/environments/staging.rb which is somehow working fine:
# Speed up asset compilation on Heroku.
config.assets.cache_store = :dalli_store
config.eager_load = true
config.stripe_livemode = false

Rails 4.2: image path and fingerprint only added when config.assets.compile = true

In production, the correct paths to my images are not called with the image tag, and the md5 fingerprint is not added. The image names (e.g. "pretty_picture.jpg") are stored in the database. The precompilation files are all present in the public folder including the manifest file.
When called with image_tag:
image_tag #test_question.question.image
I get:
<img src="/images/pretty_picture.jpg">
If I set config.assets.compile = true in production.rb the image is rendered and I get:
<img src="/assets/images/pics/pretty/pretty_picture-e0df5012b6930cda4efaa866af22a63f.jpg" >
My hack solution is to use (in HAML)
%img{src: "/assets/"+Rails.application.assets.find_asset(#test_question.question.image).digest_path}
In production.rb I have
config.assets.digest = true
config.assets.enabled = true
config.serve_static_files = false
config.assets.compile = false
Setting the config.assets.compile to true in production is not recommended. This seems like very strange behaviour on behalf of sprockets and the asset pipeline. Any idea what is wrong with the use of image_tag here?
In production, you should precompile the assets before starting the server, with the following command (and automate it to do it every time you deploy):
rake assets:precompile RAILS_ENV="production"
and keep config.assets.compile = false in your production.rb. Check in the Asset Pipeline Guide.

Static assets aren't up to date in production

I have a static asset being served at /assets/images/example.svg. I made a change to this SVG in the codebase, and pushed to production.
It's not updated because I have static asset caching set up:
config.serve_static_assets = true
config.cache_store = :redis_store, "#{ENV['OPENREDIS_URL']}/0", { expires_in: 90.minutes }
And here's the response headers for the asset:
I've run this command, which still doesn't expire my static assets:
heroku run rake tmp:cache:clear assets:clean:all assets:precompile
I've tried incrementing the config.assets.version, which didn't work either:
config.assets.version = '1.1'
How do you deal with static assets changing in the codebase? How do I manually expire my redis cache for a specific asset, or in general?
I'd just wait another 30 minutes for it to expire. From what I understand, when you serve static assets on heroku you lose out on fingerprinting, meaning the cache would not be invalidated even if you change your assets version.
I recently went the way of putting everything on S3 using the asset_sync gem. Everything has been incredibly peachy ever since.
Here's the configuration if you choose to go that route:
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Enable assets
config.assets.enabled = true
# Generate digests for assets URLs
config.assets.digest = true
config.action_controller.asset_host = "//your-bucket.s3.amazonaws.com"
In /config/environments/production.rb , try setting:
config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true

rails no route matches after assets:precompile

I am running Rails 3.2.8 application in production mode.
I have routing problems after i have done "rake assets:precompile".
My log message is :
ActionController::RoutingError (No route matches [GET] "/corp/assets/application-cf24b2a92e88a02835248f85a9f3c462.css"):
This file exists and it is in current location.
My routes are under scope "corp".
My config "config/application.rb" have option "config.assets.enabled = true".
My config "config/environments/production.rb" have following options:
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
Application works fine in development mode.
Before that assets:precompile everything was fine.
After few hours of searching of posts i can't find any solution to my problem.
Please help me fix this!
In production mode, Rails will not be responsible for serving static assets. Therefore, you are getting this error. This is controlled by this setting in config/environment/production.rb in your application:
config.serve_static_assets = false
You can either set to that true or use a real server like Apache or Nginx which will serve the static assets. I suspect Pow may also do it.
Update
try this
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = false

Rails 3.1 Production - Javascript is missing .js endings on deployment

I've just made my Rails app and deployed it to Heroku. A very weird thing happened in the process though. Half of my javascript-files are missing their endings (.js). I have absolutely no idea why this is.I've searched far and wide, but I don't seem to find an answer.
My production-config looks like this:
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
config.assets.precompile << '*.js'
Any suggestions?
Posted this other similar question 1 hour ago (http://stackoverflow.com/questions/9049023/rails-3-1-production-javascript-loads-but-doesnt-execute). Sorry for the spamming.
Does this help? You didn't show where your js files are included. If they have dots in the name, apparently you need to tell Rails.
asset:precompile for .js files? rails 3.1

Resources