I'm running the latest version of Thin, Rails, and Ruby. The relevant parts of my build script are:
export RAILS_ENV=production
export RAILS_SERVE_STATIC_FILES=true
# generate static assets
RAILS_ENV=production rake assets:precompile
# restart server
rails server thin -d
And in my production.rb I have
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
And I include these files in my view using
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
This successfully compiles my assets and moves them to the /public/assets folder. However, none of these assets are actually served when loading the page. The generated HTML is
<link rel="stylesheet" media="all" href="/stylesheets/application.css" />
<script src="/javascripts/application.js"></script>
These don't have the digests included in the filename, so I believe they're incorrect. Attempting to manually load the filenames both with and without their digest included all fail with a 404 as well. What am I doing wrong?
put the js files under app/assets/javascript.
put the css files under app/assets/stylesheets.
All your js and css files are loaded in the header of application.html.erb, which loads the application.css and application.js. This is what these two lines of code does.
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
These two files, there is a line require_tree . which loads all the files in the corresponding directory:
app/assets/javascript
app/assets/stylesheets
I usually use public folder only for external libraries when I am not using a CDN. And you will need to explicitly include them in the application.js and application.css file.
Related
In my application, I have many controllers and tons of css which could potentially be conflicting if plopped all together. Because of this I am linking my stylesheets on a controller-to-controller basis. The Ruby on Rails Guides suggests the following:
You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following:
%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag
params[:controller] %>
(I had to omit the first < from <%= because of Stack Overflow block quotes.)
Therefore, here is the resulting head of my application.html.erb file:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag params[:controller] %>
<%= stylesheet_link_tag params[:controller] %>
I have shared css for a navigation bar, which is #import[ed] in my application.css file, and that always works fine. The navigation bar is always styled properly.
The problem is, when I visit a specific page, on the first page request, the relevant css to that controller isn't loaded. And in the logs, a request is never made for the controller's stylesheets. But on the second page request, it gets styled, and the logs reveal that the stylesheets were requested.
Any reason why this is?
Below is some relevant code to this issue:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*/
#import 'normalize';
#import 'sass_vars';
#import 'sitewide';
#import "https://fonts.googleapis.com/css?family=Nunito:300,400,700";
And in my config/initializers/assets.rb I added all of the names of my javascript and css files to the line
Rails.application.config.assets.precompile += ......
If you are not using Turbolinks, remove it from your project. It includes:
removing turbolinks gem
removing requires from application.js and application.css
removing data-turbolinks-track attribute from your layout
And consider using rails new --skip-turbolinks for your next project.
Ok so compiling my assets is working fine but when I run:
thin start -e production
none of my javascript or css is loading. My browser is also cancelling the requests to get my assets. I'm not sure why this is but I suspect its because it thinks its 404'ing on them. If you look at the top image you'll see that my application.css file was compiled and stored in my assets folder but when I try to access the file, I'm getting my 404.html file.
What gives!?
Edit:
I was asked to post my view. Here is some of the code in the project:
<% content_for :title, 'Quick Quote' %>
<% content_for :subtotal, get_subtotal %>
<% content_for :stylesheet_includes do %>
<%= stylesheet_link_tag "quote", "jquery-ui-timepicker-addon" %>
<% end %>
<% if #quote.errors.any? %>
<div class="flash alert">
<% #quote.errors.full_messages.each do |msg| %>
<div><%= msg %></div>
<% end %>
</div>
<% end %>
<h1>Quick Quote</h1>
my layout:
<!DOCTYPE html>
<html>
<head>
<title><%= (yield(:title) + " - " unless yield(:title).blank?).to_s + "Online Scheduler Order Taker" %></title>
<%= stylesheet_link_tag "application", "jquery-ui-timepicker-addon.css", :media => "all" %>
<%= yield :stylesheet_includes %>
<%= javascript_include_tag "application" %>
<%= yield :javascript_includes %>
<%= csrf_meta_tags %>
</head>
<body>
code in my production.rb
config.assets.precompile += %w( quote.css jquery-ui-timepicker-addon.css prices.css contact.css )
Top of my application.css.scss.erb:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*
* require_tree . <- commented out
* require jquery-ui-timepicker-addon.css <- commented out
*/
my entire application.js file
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery-ui-timepicker-addon.js
//= require_tree .
Check your config/environments/production.rb file and add next line to it (if it does not have it yet):
config.serve_static_assets = true
Rails recommends that this setting config.serve_static_assets by default should be disabled i.e. set to false. Here is the default configuration in config/environments/production.rb generated in rails app
Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
So if you are setting it to true in your local app then that's still fine. But if you are deploying your app on Apache or ngix or anything other than heroku then its not advisable to make config.serve_static_assets=true in your production.rb config file. Here is the documentation in Rails guides.
config.serve_static_files configures Rails itself to serve static
files. Defaults to true, but in the production environment is turned
off as the server software (e.g. NGINX or Apache) used to run the
application should serve static assets instead. Unlike the default
setting set this to true when running (absolutely not recommended!) or
testing your app in production mode using WEBrick. Otherwise you won't
be able use page caching and requests for files that exist regularly
under the public directory will anyway hit your Rails app.
URL - http://guides.rubyonrails.org/configuring.html
Rails 5:
Change config.public_file_server.enabled in production.rb to true or add RAILS_SERVE_STATIC_FILES with any value to env variables.
Reference: https://github.com/rails/rails/pull/18100
Can you put this line in your current environment.rb?
config.serve_static_assets=true
Reference: here
Here's the error:
Processing by LandingPageController#index as HTML
Rendered landing_page/index.html.erb within layouts/application (2.1ms)
Completed 500 Internal Server Error in 49ms
ActionView::Template::Error (landing_page.css isn't precompiled):
12: <![endif]-->
13:
14: <%= stylesheet_link_tag "application", :media => "all" %>
15: <%= stylesheet_link_tag params[:controller] %>
16:
17: </head>
18: <body>
app/views/layouts/application.html.erb:15:in `_app_views_layouts_application_html_erb__3002306950342527375_29178380'
I can see that it's looking for the landing_page.css file because of line 15. What I don't understand is what is what is the best way to have this asset precompiled for production. I have tried modifying /config/application.rb:
config.assets.precompile += ['landing_page.css']
This doesn't seem right to me. I'd have to do this for every single stylesheet which would be annoying.
I added code to the application.css manifest:
*= require landing_page
This doesn't seem to work. I get the first error when I do this and don't modify the application config file.
I'm stumped as to how you can include the line
<%= stylesheet_link_tag params[:controller] %>
in your layout and have your assets precompiled for production when you run
bundle exec rake assets:precompile
I feel like I'm missing some simple trick that automatically adds the auto-generated stylesheets and javascript files to the list of files to be precompiled when you run the rake task.
Adding require landing_page to application.css does not cause landing_page to be precompiled. It means that when application.css is precompiled, the contents of landing_page.css will be included in the output.
If you are going to load them individually, ie
<%= stylesheet_link_tag params[:controller] %>
Then you will need to add them all to the list of things to precompile. You can use wildcards in that list, so if these controller specific stylesheets were all in stylesheets/controllers then you could do
config.assets.precompile += ["controllers/*.css"]
Typically though people tend not to do this. While for ease of development things are often split up on a per controller basis, all of these are then required from application.css. Application.css is then the only stylesheet you call stylesheet_link_tag on
in your /config/environments/production.rb add this:
config.assets.precompile += %w( landing_page.css)
This was news to me so I'll share: In Rails 4, only application.js|css manifest files files are auto-compiled by Rails. If you're using something like
javascript_include_tag controller_name
# ex javascript_include_tag 'guitars'
you apparently need to add your custom manifests ('guitars') to config.assets.precompile as mentioned above. I {believe} this is a change from Rails 3.2.
I am reading the "agile web development with rails" book and ran into a problem at the end of chapter 6. Basically, what I have done so far is defined a sass stylesheet (products.css.scss) and linked it to my application in layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Depot</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body class='<%= controller.controller_name %>'>
<%= yield %>
</body>
</html>
but it doesn't load when i run the server and visit the page!
any idea why?
This solved my problem:
bundle exec rake assets:precompile
i just spent an hour figuring out the answer, since been having the exact same issue myself. go to your application.css.sass and make sure it has
/* ...
*= require_self
*= require_tree .
*/
in it. this auto loads all the other .css.sass in apps/assets/stylesheets, then precompiles them into public/assets/stylesheets to be 1 statis css file, and that is being served to your browser.
Have you include that products.css.scss in the application.css manifest file that you are referencing in the layout?
With the asset pipeline enabled, you must include the manifest in the layout and reference all the stylesheets from the manifest.
Hope it helps.
Running the assets precompile command will compile as the user above mentioned... however, that might not be what you want to do unless you want to have to run that everytime you make a change and then add all of this to your SCM repo and then possibly have issues in production.
The real solution to this exact example is that the doesn't have the "products" class in it, so the products.css.scss isn't picked up. See this post which helped me understand this: https://stackoverflow.com/a/10080134
place the depot.css from /public/stylesheets/ to app/assets/stylesheets/
according to the documentation (http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/StylesheetTagHelpers/stylesheet_link_tag) you can set the tag to include all stylesheets in the stylesheets directory with stylesheet_link_tag :all There are also options for caching, recursion, and concatenation.
If you don't want to include everything but just the application.css and your controller's css you could do this:
= stylesheet_link_tag 'application', params[:controller].classify.downcase.pluralize
I've been following along Michael Hartl's excellent RoR Tutorial, but I'm using RoR 3.1. I am a newbie to RoR 3.1 and need help related to assets pipeline. Here is my problem:
Before continuing to section 5.3, I thought I'd like to push to Heroku and see how things develop. To my surprise "GET /" results in error 500. Everything went OK in my local-development-environment. I then tried running my local sample_app under production-environment (rails s -e production). Same result, error 500:
Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Pages#home
Showing /Users/john/Projects/sample_app/app/views/layouts/_stylesheets.html.erb where line #4 raised:
blueprint/screen.css isn't precompiled
Supporting info:
I put blueprint CSS directory under vendor/assets/stylesheets.
I followed Michael's section 13.1.4 advice and have the following as my app/views/layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= render 'layouts/stylesheets' %>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
<%= yield %>
</section>
<%= render 'layouts/footer' %>
</div>
</body>
</html>
Content of app/views/layouts/_stylesheets.html.erb:
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
I have run bundle exec rake assets:precompile.
Content of public/assets/manifest.yml:
---
logo.png: logo-8e0a5ad292fbb13a2b07e68fa3995406.png
rails.png: rails-bd9ad5a560b5a3a7be0808c5cd76a798.png
blueprint/plugins/buttons/icons/cross.png: blueprint/plugins/buttons/icons/cross-2ebcd25368006d1b7b0c5b7d6b523ab3.png
blueprint/plugins/buttons/icons/key.png: blueprint/plugins/buttons/icons/key-55237526967cbcab3e8cfb12f0029d88.png
blueprint/plugins/buttons/icons/tick.png: blueprint/plugins/buttons/icons/tick-3f5fc1f52b505b93f88263e0432d25ce.png
blueprint/plugins/buttons/readme.txt: blueprint/plugins/buttons/readme-3ff7f5dbb0288d71f70682fdbe9d86ec.txt
blueprint/plugins/fancy-type/readme.txt: blueprint/plugins/fancy-type/readme-e7ed185d1a9f23256d418ab929b464d9.txt
blueprint/plugins/link-icons/icons/doc.png: blueprint/plugins/link-icons/icons/doc-b071fd74b88ff38cda8360a53f493013.png
blueprint/plugins/link-icons/icons/email.png: blueprint/plugins/link-icons/icons/email-28104e72b3418737d4b9b329c12ec358.png
blueprint/plugins/link-icons/icons/external.png: blueprint/plugins/link-icons/icons/external-ee6d976ddb80125fafe1a33c6f8aed10.png
blueprint/plugins/link-icons/icons/feed.png: blueprint/plugins/link-icons/icons/feed-59bc8604661681639d25cb7015a32c38.png
blueprint/plugins/link-icons/icons/im.png: blueprint/plugins/link-icons/icons/im-afeeb6e0b652c1edb1441bf0fb428596.png
blueprint/plugins/link-icons/icons/lock.png: blueprint/plugins/link-icons/icons/lock-d73c4b3b57ce72cb6dbd8b265507ff75.png
blueprint/plugins/link-icons/icons/pdf.png: blueprint/plugins/link-icons/icons/pdf-c4c543e5103a8516839a7846b91e1ac4.png
blueprint/plugins/link-icons/icons/visited.png: blueprint/plugins/link-icons/icons/visited-fb2370448bc4ea5d079e963a8c0d900b.png
blueprint/plugins/link-icons/icons/xls.png: blueprint/plugins/link-icons/icons/xls-5399729cd31dffc492a04b3805cd0be1.png
blueprint/plugins/link-icons/readme.txt: blueprint/plugins/link-icons/readme-42c02030199cd36a671d4b623cb4dc36.txt
blueprint/plugins/rtl/readme.txt: blueprint/plugins/rtl/readme-8d11bf76e19fb3fc7dbc6c2ddb54b92d.txt
blueprint/src/grid.png: blueprint/src/grid-973add038ed86febca85f03e8b35b94a.png
jquery-ui.min.js: jquery-ui-7e33882a28fc84ad0e0e47e46cbf901c.min.js
jquery.min.js: jquery-8a50feed8d29566738ad005e19fe1c2d.min.js
application.js: application-a552e1db33b8be6a42eedf1261916f3c.js
application.css: application-214e0c0742f20b334e8a7776e0a4c71d.css
I don't see blueprint/screen.css in manifest.yml.
What am I missing?
From http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array
This means that in your config/environments/production.rb, you set
config.assets.precompile += %w( blueprint/screen.css blueprint/print.css )
or a catchall:
config.assets.precompile += %w( *.css *.js )
I added a reference to the blueprint stylesheets in my application.css file. This way, I do not have to change the layout to modify the stylesheets, I simply need to modify the application.css file, run rake assets:precompile, and restart the webserver (if webrick or similar).
My application.css currently looks like this:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree ./blueprint
*= require_tree .
*/
You may want to precompile your assets on production as described above.
And if you use capistrano you may do it after code update:
require 'bundler/capistrano'
after 'deploy:update_code' do
run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
end
From my own experience with this problem, where I was also going through the excellent RoR tutorial by Michael Hartl, there was one step that I think might be left out at this point...
bundle exec rake assets:precompile
git add public/assets
git commit -m "vendor compiled assets"
git push heroku
I found this here.