Rails 3.2 precompile compiles image but ActionView does not pull it - ruby-on-rails

I am deploying a RoR 3.2 app to a shared server.
When I run rake assets:precompile RAILS_ENV=production I can see at ~/public_html/my_app/assets/ that all my images are precompiled.
But when I go to mywebsite.com ActionView throws an error. The production log gives me:
ActionView::Template::Error (200/adrap21.png isn't precompiled).
Funny thing is that at ~/public_html/my_app/assets/200 I can see a precompiled image: adrap21-a5f042dd2b89a3d87eba25969495d678.png
I have run several times one of the following:
1) rake assets:precompile RAILS_ENV=production
2) RAILS_ENV=production rake assets:precompile
at my_app/config/environments/production.rb I have the following:
# 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 = true
I have changed config.assets.compile from false to true and does not help.
I have read related answers but other people solutions are not working for me.
This one was suposed to be foolproof. Another one, more specific about not compiling images, this other prevents using config.assets.compile = true.
I have even tried the advice of using config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] , but it is not yet working. Might there be something wrong with images call? Is it a RoR 3.2 bug?

FOUND WHAT THE PROBLEM WAS.
Phusion Passenger needs to be restarted for the compiled assets to work. That is why, using Capistrano, you need to restart the server for all the compiled to work together.
Thanks again, Paritosh. I know too how slow is learning curve at beginning when you learn to program by yourself.

Related

Rails 5 - config.assets.compile should be true - why?

I am developing Rails 5 application and use assets pipeline.
It work well in development mode, but if I try to run it in production mode, it can't load images & styles correctly.
I checked and found that it is because
config.assets.compile = false
in config/environments/production.rb
Unless I set it true, it doesn't work at all.
I know live compilation isn't good for production, what is solution?
There are two options related to serving assets within a Rails server:
Asset compilation
config.assets.compile = true
refers to asset compilation. That is, whether Rails should recompile the assets when it detects that a new version of the source assets is there. In development, you want to have it set to true, so that your styles get compiled when you edit the css files. With the next request, Rails will automatically recompile the assets. On production, you usually want to set it to false and handle asset compilation during deployment. For this, you have to run
RAILS_ENV=production bin/rails assets:precompile
Usually, if you deploy using Capistrano, it takes care of that.
Asset serving
The second option related to assets is
config.public_file_server.enabled
This describes whether it is Rails that should serve the compiled files from the public/assets directory. In development, you want that, so it's true by default. In production, you usually don't want to fire up your web server to serve the logo image or a css file, so you probably compile the assets and then host them separately (for example, on a CDN like cloudfront). If you still want them to be served in production, you can launch Rails with:
RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production bin/rails server
Precompile your assets first.
Run RAILS_ENV=production rake assets:precompile to generate your stylesheets and js files in your public directory.

Assets (images, JS and CSS) not getting served after upgrading to Rails 4.2.0 in production

The error that I am getting is
"No route matches [GET]...."
I have already tried doing following things in environments/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
config.assets.precompile = ['*.js', '*.css']
PS : I am using Apache2 for hosting my rails app
I've had more issues upgrading from rails 4.1 to rails 4.2 then I've had upgrading between any other version of rails ever. I use nginx, but go ahead and please try the following:
prefix the following commands with RAILS_ENV=production and run rake tmp:clear, rake asset:clean and rake asset:precompile
config.assets.compile = false
config.serve_static_files = false
This was the issue
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
I did not have and environment variable and it was causing an issue.
For people who land upon this question

Rails asset pipeline: images not looking up resource with build id

I'm getting a 404 on all of the images included with jquery-ui-rails on Rails 4.0.1 after going production. It works fine in development environment. The site is looking for /assets/jquery-ui/ui-icons_222222_256x240.png, but only public/assets/jquery-ui/ui-icons_222222_256x240-890385424135de1513f00cbecfb7f990.png exists in the filesystem. How come production build IDs are not being appended?
I've also had this problem with some fonts. For the time being I've worked around it by just manually copying and pasting to the sought path.
First thing to try is precompiling assets specifically for the production environment:
RAILS_ENV=production rake assets:precompile
If that doesn't do anything, set the following in production.rb and precompile again
config.assets.precompile += ['*.js', '*.css']
config.assets.compile = true

Rails 4: assets not loading in production

I'm trying to put my app into production and image and css asset paths aren't working.
Here's what I'm currently doing:
Image assets live in /app/assets/images/image.jpg
Stylesheets live in /app/assets/stylesheets/style.css
In my layout, I reference the css file like this: <%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
Before restarting unicorn, I run RAILS_ENV=production bundle exec rake assets:precompile and it succeeds and I see the fingerprinted files in the public/assets directory.
When I browse to my site, I get a 404 not found error for mysite.com/stylesheets/styles.css.
What am I doing wrong?
Update:
In my layout, it looks like this:
<%= stylesheet_link_tag "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
The generate source is this:
<link data-turbolinks-track="true" href="/stylesheets/bootstrap.min.css" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/stylesheets/styles.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-0c647c942c6eff10ad92f1f2b0c64efe.js"></script>
Looks like Rails is not properly looking for the compiled css files. But it's very confusing why it's working correctly for javascripts (notice the /assets/****.js path).
In rails 4 you need to make the changes below:
config.assets.compile = true
config.assets.precompile = ['*.js', '*.css', '*.css.erb']
This works with me. use following command to pre-compile assets
RAILS_ENV=production bundle exec rake assets:precompile
Best of luck!
I just had the same problem and found this setting in config/environments/production.rb:
# Rails 4:
config.serve_static_assets = false
# Or for Rails 5:
config.public_file_server.enabled = false
Changing it to true got it working. It seems by default Rails expects you to have configured your front-end webserver to handle requests for files out of the public folder instead of proxying them to the Rails app. Perhaps you've done this for your javascript files but not your CSS stylesheets?
(See Rails 5 documentation). As noted in comments, with Rails 5 you may just set the RAILS_SERVE_STATIC_FILES environment variable, since the default setting is config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?.
In /config/environments/production.rb I had to add this:
Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.
It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.
I was able to solve this problem by changing:
config.assets.compile = false to
config.assets.compile = true in /config/environments/production.rb
Update (June 24, 2018): This method creates a security vulnerability if the version of Sprockets you're using is less than 2.12.5, 3.7.2, or 4.0.0.beta8
For Rails 5, you should enable the follow config code:
config.public_file_server.enabled = true
By default, Rails 5 ships with this line of config:
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
Hence, you will need to set the environment variable RAILS_SERVE_STATIC_FILES to true.
There are 2 things you must accomplish to serve the assets in production:
Precompile the assets.
Serve the assets on the server to browser.
1) In order to precompile the assets, you have several choices.
You can run rake assets:precompile on your local machine, commit it to source code control (git), then run the deployment program, for example capistrano. This is not a good way to commit precompiled assets to SCM.
You can write a rake task that run RAILS_ENV=production rake assets:precompile on the target servers each time you deploy your Rails app to production, before you restart the server.
Code in a task for capistrano will look similar to this:
on roles(:app) do
if DEPLOY_ENV == 'production'
execute("cd #{DEPLOY_TO_DIR}/current && RAILS_ENV=production rvm #{ruby_string} do rake assets:precompile")
end
end
2) Now, you have the assets on production servers, you need to serve them to browser.
Again, you have several choices.
Turn on Rails static file serving in config/environments/production.rb
config.serve_static_assets = true # old
or
config.serve_static_files = true # new
Using Rails to serve static files will kill your Rails app performance.
Configure nginx (or Apache) to serve static files.
For example, my nginx that was configured to work with Puma looks like this:
location ~ ^/(assets|images|fonts)/(.*)$ {
alias /var/www/foster_care/current/public/$1/$2;
gzip on;
expires max;
add_header Cache-Control public;
}
Rails 4 no longer generates the non fingerprinted version of the asset: stylesheets/style.css will not be generated for you.
If you use stylesheet_link_tag then the correct link to your stylesheet will be generated
In addition styles.css should be in config.assets.precompile which is the list of things that are precompiled
change your Production.rb file line
config.assets.compile = false
into
config.assets.compile = true
and also add
config.assets.precompile = ['*.js', '*.css', '*.css.erb']
What you SHOULD NOT do:
Some of my colleagues above have recommended you to do this:
config.serve_static_assets = true ## DON”T DO THIS!!
config.public_file_server.enabled = true ## DON”T DO THIS!!
The rails asset pipeline says of the above approach:
This mode uses more memory, performs more poorly than the default and is not recommended. See here: (http://edgeguides.rubyonrails.org/asset_pipeline.html#live-compilation)
What you SHOULD do:
Precompile your assets.
RAILS_ENV=production rake assets:precompile
You can probably do that with a rake task.
I'm running Ubuntu Server 14.04, Ruby 2.2.1 and Rails 4.2.4 I have followed a deploy turorial from DigitalOcean and everything went well but when I go to the browser and enter the IP address of my VPS my app is loaded but without styles and javascript.
The app is running with Unicorn and Nginx. To fix this problem I entered my server using SSH with my user 'deployer' and go to my app path which is '/home/deployer/apps/blog' and run the following command:
RAILS_ENV=production bin/rake assets:precompile
Then I just restart the VPS and that's it!
It works for me!
Hope it could be useful for somebody else!
If precompile is set you DO NOT need
config.assets.compile = true
as this is to serve assets live.
Our problem was we only had development secret key base set in config/secrets.yml
development:
secret_key_base: '83d141eeb181032f4070ae7b1b27d9ff'
Need entry for production environment
The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files (this will include all image assets automatically) from app/assets folders including your gems:
If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb:
Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
First of all check your assets, it might be possible there is some error in pre-compiling of assets.
To pre-compile assets in production ENV run this command:
RAILS_ENV=production rake assets:precompile
If it shows error, remove that first,
In case of "undefined variable" error, load that variable file before using it in another file.
example:
#import "variables";
#import "style";
in application.rb file set sequence of pre-compiliation of assets
example:
config.assets.precompile += [ 'application.js', 'admin.js', 'admin/events.js', 'admin/gallery.js', 'frontendgallery.js']
config.assets.precompile += [ 'application.css', 'admin.css','admin/events.css', 'admin/gallery.css', 'frontendgallery.css']
Found this:
The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role.
in config/environments/production.rb:
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
So set env RAILS_SERVE_STATIC_FILES or using Nginx to serving static files.
Add config.serve_static_assets = true will still work, but removed in future.
it is not recommended to let capistrano do assets precompile, because it may take ages and often time out. try to do local assets precompile.
1st, set in config/application.rb
config.assets.initialize_on_precompile = false
then do local
RAILS_ENV=production bin/rake assets:precompile
and add those public/assets to git.
and config/environments/development.rb, change your asset path to avoid using precompiled assets:
config.assets.prefix = '/dev-assets'
If you have db connection issue, means u have initializer that uses db. one way around it is to set a new environment by duplicate production.rb as maybe production2.rb, and in database.yml, add production2 environment with development db setting. then do
RAILS_ENV=production2 bin/rake assets:precompile
if you are still facing some issue with assets, for example ckeditor,
add the js file into config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( ckeditor.js )
I may be wrong but those who recommend changing
config.assets.compile = true
The comment on this line reads: #Do not fallback to assets pipeline if a precompiled asset is missed.
This suggests that by setting this to true you are not fixing the problem but rather bypassing it and running the pipeline every time. This must surely kill your performance and defeat the purpose of the pipeline?
I had this same error and it was due to the application running in a sub folder that rails didn't know about.
So my css file where in home/subfolder/app/public/.... but rails was looking in home/app/public/...
try either moving your app out of the subfolder or telling rails that it is in a subfolder.
location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
}
This fixed the problem for me in production. Put it into the nginx config.
Even we faced the same problem where RAILS_ENV=production bundle exec rake assets:precompile succeeded but things did not work as expected.
We found that unicorn was the main culprit here.
Same as your case, even we used to restart unicorn after compiling the assets. It was noticed that when unicorn is restarted, only its worker processes are restarted and not the master process.
This is the main reason the correct assets are not served.
Later, after compiling assets, we stopped and started unicorn so that the unicorn master process is also restarted and the correct assets were getting served.
Stopping and starting unicorn brings around 10 secs on downtime when compared to restarting the unicorn. This is the workaround that can be used where as long term solution is move to puma from unicorn.

Assets pipeline when updating to Rails 3.1 on Heroku

I've just upgraded my app on Heroku from Rails 3.0 to 3.1, and I'm trying to make the assets pipeline work. The main issue is that I can read from the heroku log the following kind of lines, for every asset:
2011-09-03T16:35:28+00:00 app[web.1]: cache: [GET /assets/border-a3c571a354b9381740db48aeebfaa63a.jpg] miss
If I understand the pipeline correctly, this should not be "miss" for every request I make from a browser, but it should be found in the cache.
Reading the Heroku docs you can find this explanation:
Rails 3.1 provides an assets:precompile rake task to allow the compilation penalty to be paid up front rather than each time the asset is requested. If this task exists in your app we will execute it when you push new code.
But how should that "assets:precompile" task be? I tried building a project with rails 3.1 from scratch to try to find out, but there is no such task in a bare project. Or am I missing something? How could I make that the assets are found in the cache? Maybe is just an issue with configuration.
These are the options of my production config file:
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = true # If I turn this off I get a 500 error and logs say that an asset isn't compiled
My application.rb has this line:
config.assets.enabled = true
Thanks a lot for your help!
Also, take a look at http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
For faster asset precompiles, you can partially load your application
by setting config.assets.initialize_on_precompile to false in
config/application.rb, though in that case templates cannot see
application objects or methods. Heroku requires this to be false
I was wondering the same thing, but here's a tip to help figure out if your assets are live-compiling or not
run rake assets:precompile locally
make some changes to your css but do not rerun the rake task
git add, commit and push to heroku
If the changes you made in step 2 show up on heroku, then you know your app is live-compiling
Don't forget that you are now in charge of http caching since Varnish is no longer included on celadon, so you need to set up rack-cache and memcached yourself:
heroku doc on http caching
setup rack-cache with memcached on heroku
heroku docs on memcached
But yeah, I found this puzzling too
Can you try with config.serve_static_assets set to true and
config.action_dispatch.x_sendfile_header = "X-Sendfile"
added to your config/environments/production.rb file?
When you push your code to Heroku you should see the precompiling announced by the slug compiler AFAICT.
Make sure you are on the Heroku "Cedar" stack. Then Heroku will automatically precompile your assets during slug compilation.
Note: I'm still getting "cache misses" too, but I don't think that's really true because your app wouldn't work if your assets weren't compiled.

Resources