rails no route matches after assets:precompile - ruby-on-rails

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

Related

Assets not rendering in production rails

I have precompiled the assets in production but the application is not able to load the assets. My app is deployed on AWS EC2
I checked in the server in public/assets folder the application.css is present.
but still it says 404 error.
My production.rb configuration is
My nginx.conf settings are:
The 404 is based on that line in your config: config.serve_static_files = true. That might be the wrong name depending on your rails version. I believe that setting is named config.serve_static_assets in Rails prior to version 5.
If you don't have a server (nginx/etc) then you want that setting to be true. Otherwise you need to reconfigure your server to point at your /public directory, and make sure rake assets:precompile runs successfully.
I had the same problem
set config.assets.compile = True
It works for me.
The problem was my configuration in nginx of root path was wrong. so assets were not pointing to wrong path.
I changed in nginx.conf:
from root /home/deploy/appname/public
to root /home/deploy/appname/current/public
AND in production.rb
config.serve_static_assets = true to config.serve_static_assets = false
I did the same as you:
root /home/deploy/appname/public root => /home/deploy/appname/current/public
And that works for me. With the unique difference that i left the config/production.rb file like this:
config.public_file_server.enabled = true
config.assets.compile = true
I'm using Rails 5.

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 compiles assets both with and without md5 hash, why?

I'm relatively new to RoR and I'm curious about why Rails compiles assets both with and without md5 hash for production?
I run bundle exec rake assets:clean then bundle exec rake assets:precompile
My production.rb file:
MyApp::Application.configure do
# 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 = 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
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
config.assets.precompile += %w(tos.js, tos.css)
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
My application works with files with hashes in their names and it's the way it should be in my case :)
So I have two questions here:
1) Why is it happening when compiled?
Rails compiles assets both with and without md5 hash for production
2) What are these files (without hashes) for?
Maybe I don't get something, so please could someone explain.
The reason it does it is so that you can access the files without knowing the MD5 fingerprint (for example in a non-rails application, or a file within the rails app which isn't compiled or run by the rails stack (e.g. a 500/502 status error page). In this case you would have to compile the assets then change the css/js links in the static HTML files each time you updated the code (thus causing a change in the MD5 hash).
So instead rails produces 2 copies of each asset file, one with the fingerprint in the filename, the other without (e.g. application-731bc240b0e8dbe7f2e6783811d2151a.css, and application.css). The fingerprinted version is obviously preferred (see 'what is fingerprinting and why should I care' in the rails asset pipeline guide). But the non-digested version is there as a fallback.
As a final thought on the matter I'd take a read of the following pull request to the rails git repo: https://github.com/rails/rails/pull/5379 where they are discussing the pros and cons of the non-digested filenames, and the possibility of being able to turn off compilation of them.
HTH

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