Rails 3.2 Assets in Production - ruby

I'm at my wits end with this. It seems all of the newer rails apps I make its set a couple of configs in environments/production.rb, deploy and move on with my life. But now we're migrating a few rails apps to a new server and it seems all of them have this issue when deploying to production.
What appears to be happening is that neither my javascripts or stylesheets are getting compiled. And I see none of the styles for the app and the javascript does not work.
config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(:default, :assets, Rails.env)
end
module MyApp
class Application < Rails::Application
... omitted code ...
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.generators.stylesheet_engine = :scss
end
end
config/environments/production.rb:
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 = true
# Generate digests for assets URLs
config.assets.digest = true
... omitted code ...
end
In my application-<...>.js:
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree
;
And my application-<...>.css is completely empty. What am I missing?

So after a long half day yesterday and about an hour today, I suddenly realized something... I ran into this before.
Sure enough... Updating rails to 3.2.14 and deploying again fixed the whole thing.

Related

Rails 4 assets not being served in production on Windows

I'm currently having the exact same problem as described in this post: Rails not serving assets in production or staging environments.
I am running Rails 4.0.4 in production environment on Windows 7 (so that could easily be the problem, can't use Linux unfortunately). I have run rake assets:clobber to make sure everything is cleaned up and and then RAILS_ENV=production rake assets:precompile and it succeeds without errors or warnings. All the files appear in my public/assets folder and using Windows explorer I can view the text in application.js and application.css, and images display correctly. However when I try to visit localhost:3001/assets/application.js it is blank, same with application.css, and image files come up with an error. I have restarted the server each time after changing settings and precompiling.
When I look at the logs it says the page renders successfully, there are no "No route matches" errors like I have seen in other posts. So the assets are being found, but for some reason they aren't being properly served.
Here is my production.rb:
ABC::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_dispatch.x_sendfile_header = "X-Sendfile"
config.serve_static_assets = true
config.action_mailer.default_url_options = { :host => '' }
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = false
config.assets.digest = true
end
Any help would be much appreciated, I've been stuck on this for nearly two days!
Just as I finished typing up this question, I finally came across another post which said the headers were being set but there was no body, which sounded like my problem. They were using nginx and fixed the problem by changing the following:
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
And seeing as I'm not using either Apache or nginx, I commented out both lines and finally my assets were served. Seems obvious in hindsight but I thought I would post this anyway in case it helps anyone else having the same problem.

How disable assets compilation on heroku?

I'm trying to deploy my rails app to heroku using this turtorial:
https://devcenter.heroku.com/articles/getting-started-with-rails4
So, I use rails 4.1.1 and ruby 2.1.1
My Gemfile has gem 'rails_12factor', group: :production inside.
My application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module Charticus
class Application Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
I created file public/assets/manifest.yml
But when I deploy app to heroku - it compile all my js-files files to application.js and all css-files application.css. And I can't see it on app.heroku.com using firebug.
What I need to do with my configurations to see all my js and css files on app.heroku.com ?
How disable assets precompiling and minification on heroku?
Help me please!
Thanks
lib/tasks/assets.rake
Rake::Task["assets:precompile"].clear
namespace :assets do
task 'precompile' do
puts "Not pre-compiling assets..."
end
end
You are done.
I compare config/environments/development.rb and config/environments/production.rb.
And make production.rb asset configs like in development.rb:
Comment this lines:
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
Then:
Push my changes to git repo git push origin master
Push changes to heroku git push heroku master
Rails 4 applications have a manifest-*.json file, not a manifest.yml file. This file is typically generated when you run rake assets:precompile , how are you compiling your assets?
Regardless, you need a file public/assets/manifest-(fingerprint).json file
Fast forward to 2018, and you would need to add the following to config/initializers/production.rb:
config.assets.enabled = false
Then you'd need to customize Heroku's Ruby Buildpack to not run the assets:precompile rake task. I won't provide a link to such a buildpack because I won't support or warrant one, but its pretty easy to find it in lib/language_pack/ruby.rb and start removing relevant code.
You'd then have to configure your Heroku app to use your new forked Buildpack instead of the default one (e.g. using heroku buildpacks).
Thats the cleanest way to disable the asset pipeline in a Heroku app w/ Rails, without resorting to overriding Rails' built-in rake tasks.
Fast forward to 2021 and Rails 6.x, if you completely removed Webpacker and Sprockets/Asset Pipeline, replace the bin/yarn file content with something like:
#!/usr/bin/env ruby
puts 'Yarn not present, nothing to do.'
#danielricecodes's advice is probably still valid but way more invasive.

Rails.3.1.1: config.assets.digest = true results in compiled css being used as a sprite's filename

I have a new-ish rails 3.1.1 app using sass/compass with compass taking care of the sprite generations for me. In development, all works just fine. However, when I deploy, I see this entry in the log file:
Compiled team/application.css (1ms) (pid 24202)
Compiled team/forms.css (0ms) (pid 24202)
Compiled team/member.css (0ms) (pid 24202)
Compiled application.css (0ms) (pid 24202)
Completed 500 Internal Server Error in 2014ms
ActionView::Template::Error (File name too long - /home/cri/webapps-releases/cri/releases/20111121225403/app/assets/images/html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote...(the rest of my compiled css goes here)
My production config (for asset stuff) is pretty vanilla wiht much of it being what was generated by rails:
# 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
# Specify the default JavaScript compressor
config.assets.js_compressor = :uglifier
# 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.debug = true
Can anybody tell me why my compiled css is being used as the filename for the sprite?
Update: Modified title to reflect what I now think I know about this issue.
I am running rails-3.1.1 with sass-3.1.10 and sass-rails-3.1.5 and compass-0.11.3.
compass-0.11.3 does not have proper asset pipeline support you are going to want to be using the >= 0.12.alpha.3 release you can get this by doing gem install compass --pre if you are still having this issue under 0.12 then it is a bug and i would be grateful if you would file a ticket on the compass github.
Try
config.assets.debug = false
Those workarounds are true, but this bug in rails was fixed in 3.1.4. Upgrading is likely your best all-around bet.

Rails asset pipeline not working in production environment?

I have recently upgraded an app from Rails 3.0 to 3.1. I have followed any instructions I could find for enabling the asset pipeline but it always fails when in the production environment:
<%= javascript_include_tag "application" %>
gives me
<script src="/javascripts/application.js" type="text/javascript"></script>
which is missing a digest and I get the following error:
cache: [GET /javascripts/application.js] miss
Started GET "/javascripts/application.js" for 127.0.0.1 at 2011-10-03 23:31:36 +0100
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):
I've tried variations of these settings in application.rb:
require File.expand_path('../boot', __FILE__)
#require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Blog
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/lib)
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.assets.enabled = true
config.assets.version = '1.0'
end
end
and full production.rb (minus some comments)
Blog::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
I have ran the rake assets:precompile task.
Am I missing any obvious steps?
Edit: Some additional details:
My assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.
I see my files generated in my public/assets directory with names and digests.
app/assets/javascripts/application.js does indeed compile to something like public/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js They end up in the public directory.
Sprockets is not getting loaded.
In an effort to remove active record in a previous version of rails (a la this question Remove ActiveRecord in Rails 3 (beta)) the require 'rails/all' was replaced by
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
What was missing here was sprockets/railtie
See the Upgrading to Rails 3.1 Railscast
Make sure your assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.
Execute rake assets:precompile
You should see files generated in your app/public/assets directory with names and digests if enabled.
app/assets/javascripts/application.js would compile to /assets/application-6ec417a53cb2bdb949966a153a61e7b1.js
If the asset is named similar to above with a digest, Production.rb should have the following config:
# Generate digests for assets URLs
config.assets.digest = true
If you look at the web page source you should see something similar to the following:
<script src="/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js" type="text/javascript"></script>
Try to manually load the file by going to http://example.com//assets/application-6ec417a53cb2bdb949966a153a61e7b1.js
The file should load, if not try checking permissions and further logs.

rake assets:precompile is slow

The command "rake assets:precompile" works very slow for me. Especially on my Amazon EC2 Micro production server which does not have a lot of processor resources. On EC2 I have to wait 1 minute or more during each deployment just for this precompile task alone. Is there a way to make it faster?
Previously I used Jammit to compress/minify css and js. Jammit worked nearly 10 times faster on the same web site and servers.
If you don't need to load the Rails environment, you should disable that with:
config.assets.initialize_on_precompile = false
EDIT: I've just written a gem to solve this problem, called turbo-sprockets-rails3. It speeds up your assets:precompile by only recompiling changed files, and only compiling once to generate all assets.
It would be awesome if you could help me test out the turbo-sprockets-rails3 gem, and let me know if you have any problems.
There is a bug in Rails 3.1.0 that includes too many files in the precompile process. This could be the reason for the slowness if you have many assets js and css assets.
The other is that Sprockets (the gem doing the compilation) is more complex and has to allow for more options - scss, coffeescript and erb. Because of this I suspect it will be slower doing just concatenation and minification.
As suggested, you could precompile the files before deploying them if this is still an issue.
My solution is to exclude application.js .css and any other application related assets from from precompilation. So that i can use rake assets:precompile once to precompile engine related assets only.
Then on each deploy i use a simple rake task to build any application related assets and merge them into manifest.yml:
namespace :assets do
task :precompile_application_only => :environment do
require 'sprockets'
# workaround used also by native assets:precompile:all to load sprockets hooks
_ = ActionView::Base
# ==============================================
# = Read configuration from Rails / assets.yml =
# ==============================================
env = Rails.application.assets
target = File.join(::Rails.public_path, Rails.application.config.assets.prefix)
assets = YAML.load_file(Rails.root.join('config', 'assets.yml'))
manifest_path = Rails.root.join(target, 'manifest.yml')
digest = !!Rails.application.config.assets.digest
manifest = digest
# =======================
# = Old manifest backup =
# =======================
manifest_old = File.exists?(manifest_path) ? YAML.load_file(manifest_path) : {}
# ==================
# = Compile assets =
# ==================
compiler = Sprockets::StaticCompiler.new(env,
target,
assets,
:digest => digest,
:manifest => manifest)
compiler.compile
# ===================================
# = Merge new manifest into old one =
# ===================================
manifest_new = File.exists?(manifest_path) ? YAML.load_file(manifest_path) : {}
File.open(manifest_path, 'w') do |out|
YAML.dump(manifest_old.merge(manifest_new), out)
end
end
end
To specify which assets to compile i use a YAML configuration file (config/assets.yml):
eg.
---
- site/site.css
- admin/admin.css
- site/site.js
- admin/admin.js

Resources