Rails Assets in Production - ruby-on-rails

I've just migrated to rails 3.1 and I'm having trouble getting started with the asset pipeline.
I ran: bundle exec rake assets:precompile RAILS_ENV=production after adding additional css files into config.assets.precompile += ['scaffold.css', 'other.css', 'other.js'] all of my files are located in the /app/asset images, javascripts, or stylesheets path.
Following the rake task, I can see the manifest.yml file but it is missing the additional css and js files I added to the config.assets.precompile
Here is my manifest.yml file that lives on prod:
connect.gif: connect-b85d46914c10cb653f3444c3529b83eb.gif connect.png:
connect-bce549a396f27cc5f7b315190ee3385a.png d.jpeg:
d-c5f2ca3025794efdc341159538f4a0bf.jpeg data.png:
data-d225a9937a1aa78805e704ada167b2d0.png fb.png:
fb-2e743f3607323f9d21dd2e96b5c95d4b.png feed.png:
feed-fd4c0dc513639edd0ba2f39a063fdc90.png grid.png:
grid-b3db76abb46477b06c15055df8412c79.png i.png:
i-5ac52498083d9dfc5f898cdf666bc9c0.png infb.png:
infb-80208a4e1bd3d2a03f23097a7d19fe7e.png r.png:
r-ba27ded8b8873c851cd9aaf8697f8092.png rails.png:
rails-e4b51606cd77fda2615e7439907bfc92.png share.jpeg:
share-47877a532c58510f38eeb534122b4846.jpeg share.png:
share-b9939bdcdf8b082eeb27697592258b9a.png signup.jpeg:
signup-23dfea4e8af95dae93ab254162e01fc2.jpeg tc.png:
tc-abb1838a7396c3a0a54e68ecb80cd225.png tweet.png:
tweet-3865676af138fa7d80e0e416210f2fa0.png yellow.png:
yellow-b2257e0f9bb17cd4b44faaee30df8fa6.png jquery.flot.js:
jquery.flot-944582c3d888856c9d82964ea8cf0bcc.js jquery-ui.min.js:
jquery-ui.min-4258e47b40673a02bedfda178628bfba.js jquery.min.js:
jquery.min-17322428695737103f15e8b9db8b5909.js application.js:
application-f71535809bb0483163e3e4d857abe6d0.js
What is going on here? I've restarted both passenger with touch restart.txt and nginx. I've read all of the documentation on this issue.
nginx error.log:
cache: [GET /] miss
ActionView::Template::Error (scaffold.css isn't precompiled):
2: %html
3: %head
4: %title redmeetsblue
5:
6: = javascript_include_tag "application.js"
7: = csrf_meta_tag
8: %script{:src => "http://connect.facebook.net/en_US/all.js#appId=143150285757259&xfbml=1"}
app/views/layouts/application.html.haml:5:in `_app_views_layouts_application_html_haml__44133251_81438200'
app/controllers/homes_controller.rb:7:in `index'

Have you set the config.assets.precompile values in application.rb ? If you set them elsewhere they won't be picked up when you precompile.

Related

All assets are not loading in production envirnoment rails 5

Anything works fine while I run my server using rails s, but while I run the server in production mode it looks like:without stylesheets and other assets. What I have to do to solve that problem?
In production.rb set
config.public_file_server.enabled = true
config.assets.compile = true
Then run the server like this
RAILS_ENV=production rails assets:precompile
RAILS_ENV=production rails server
Did you precompile your assets? Try running locally:
RAILS_ENV=production bundle exec rake assets:precompile
A public/assets directory will be created and then commit changes to your repository.
The assets should now be detected on your production server.
Can you provide us with an example how you are linking to CSS and JS files? It should look like this ( this is in .haml not .erb)
= stylesheet_link_tag 'default/application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => 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.

Error "not precompiled" js file on production trying to render PDF via wicked_pdf in Rails

I have troubles with wicked_pdf on production server. I have such code here in _results.pdf.haml:
#container
= wicked_pdf_javascript_include_tag "pdf_application"
:javascript
$(document).ready(function(){drawPDFCharts(#{generate_javascript_for_pdf_generator(#poll.answer_count_per_question)}, "container")});
here is pdf_application.js
//= require jquery
//= require highcharts
//= require poll-pdf-chart-generation.js.coffee
On local machine and staging everithing just fine, but on production there is an error:
Started GET "/121-consumer-electronics.pdf" for 94.76.74.170 at 2013-01-16 13:52:53 +0000
Processing by PollsController#show as PDF
Parameters: {"id"=>"121-consumer-electronics"}
***************WICKED***************
Rendered polls/_results.pdf.haml (145.7ms)
Completed 500 Internal Server Error in 816ms
ActionView::Template::Error (pdf_application.js isn't precompiled):
8:
9: #container
10:
11: = wicked_pdf_javascript_include_tag "pdf_application"
12: :javascript
13: $(document).ready(function(){drawPDFCharts(#{generate_javascript_for_pdf_generator(#poll.answer_count_per_question)}, "container")});
Here is my production.rb:
# 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 = false
# Generate digests for assets URLs
config.assets.digest = true
I cant figure out how to make it work, I saw some similar questions here, And I tried to add this file to precompile array and restart the server, but it didnt help. As I get I have compiling assets on local machine, and no compiling on production, and somehow this file is not precompiled when I try to run it...
Which file contains the javascript code that you would like to use? Is it poll-pdf-chart-generation.js.coffee?
I am unsure if wicked pdf can use coffee files but someone can please correct me if I am wrong.
I believe a workaround solution is to try converting poll-pdf-chart-generation.js.coffee to a js file and adding it to production.rb:
config.assets.precompile += ['poll-pdf-chart-generation.js']
Reference: Rails guide on asset pipeline
Change your wickedpdf include tag to look at the precompiled js asset:
wicked_pdf_javascript_include_tag "poll-pdf-chart-generation"
And you will also need to precompile your assets:
bundle exec rake assets:precompile
Sounds like the .js file was changed and not recompiled. Rails 3 compiles js, css, and stylesheets into a single file to be sent once to the browser. If anything in the assets directory changes, the whole thing has to be recompiled. Try
bundle exec rake assets:precompile
Good luck
Bob

Heroku: The page you were looking for doesn't exist

I get this error when i try to deploy my app to Heroku. My first deployment works fine before I'm doing heroku run rake db:reset.
After this, I have errors below: "We're sorry, but something went wrong." and "The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved."
My heroku logs says:
2012-12-08T11:40:54+00:00 app[web.1]: ActionView::Template::Error (bootstrap.css isn't precompiled):
2012-12-08T11:40:54+00:00 app[web.1]: 9: <%= csrf_meta_tags %>
2012-12-08T11:40:54+00:00 app[web.1]:
2012-12-08T11:40:54+00:00 app[web.1]: 8: <%= javascript_include_tag "bootstrap", media: "all"%>
Some one, Can you help me ?
Looks like Heroku is complaining that your assets are not precompiled. I'd read through this tutorial about rails on heroku, there is a section dedicated to resource pre-compilation.
You can tell your application to precompile assets in production
#config/environments/production.rb
config.assets.compile = true
# Heroku also requires this to be false
config.assets.initialize_on_precompile=false
Or you can precompile your assets using the rake task
#before pushing to Heroku and then you can push
rake assets:precompile
#or after you've pushed to heroku
heroku run rake assets:precompile

Heroku *.js isn't precompiled error

I'm using Rails on Heroku Cedar stack and it's not throwing any issues while compiling the assets but then I get a 500 internal server error:
2012-06-25T23:22:59+00:00 app[web.1]: ActionView::Template::Error (bootstrap-datepicker.js isn't precompiled):
Any idea what might be causing this? This is the javascript file I'm including (except I downloaded it locally) https://github.com/eternicode/bootstrap-datepicker/blob/master/js/bootstrap-datepicker.js
I'm including it in my application.html.erb like so:
<%= javascript_include_tag "bootstrap-datepicker" %>
Is it because its not a coffeescript file? Any help is appreciated!
I figured it out. I had to add it to production.rb in config.assets.precompile
In production.rb I added it to my config.assets.precompile:
config.assets.precompile += %w( jquery.dataTables.min.js jquery-ui-1.8.21.custom.min.js jquery-ui-1.8.21.custom.css bootstrap-datepicker.js fullcalendar.js)

Resources