Rails asset pipeline not working in production environment? - ruby-on-rails-3.1

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.

Related

How to suppress warning on PaperTrail gem with Sinatra app?

DEPRECATION WARNING: PaperTrail.track_associations has not been set. As of PaperTrail 5, it defaults to false. Tracking associations is an experimental feature so we recommend setting PaperTrail.config.track_associations = false in your config/initializers/paper_trail.rb . (called from require at /Users/george/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68)
Run options:
Since this is not a Rails app, there is no config/initializers/paper_trail.rb. Looked at https://github.com/airblade/paper_trail/blob/master/lib/generators/paper_trail/install_generator.rb, but didn't see a generator for the config file.
If I do
require 'paper_trail'
PaperTrail.config.track_associations = false
it still emits the warning.
Also tried:
def PaperTrail; end
PaperTrail.config.track_associations = false
require 'paper_trail'
This is a "Classic" Sinatra app.
The reason why this is happening is due the the call to require 'paper_trail' initializing a PaperTrail.config object: this line results in this file getting required, a part of which gets executed resulting in initializing the config singleton. We can solve this by first setting up the config object and then requiring the top-level paper_trail file later.
# app.rb
require 'sinatra'
require 'paper_trail/config'
config = PaperTrail::Config.instance
config.track_associations = true
# require models here
Dir["#{Dir.pwd}/models/*.rb"].each { |file| require file }
# rest of app code
And the models won't need any change:
# models/article.rb
require 'paper_trail'
class Article < ActiveRecord::Base
has_paper_trail
end
Thus, when the require 'paper_trail' call gets executed, we already have the correct configuration setup and the warning won't be displayed.
I added PaperTrail.config.track_associations = false to config/application.rb in the definition of class Application.

Rails 3.2 Assets in Production

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.

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.

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

Gem testing with Rspec

I have written a custom Ruby Gem to hook into our company's authentication and authorization system and am starting to develop the unit tests for the gem.
In our rails app, the Gem can be configured via environment.rb and a custom initializer and yaml file containing the configuration values.
I need to translate the configuration of the Gem in rails to test the standalone Gem. How do I translate this over to Rspec to perform integration testing??
Gem configuration in rails
# environment.rb
MyGem.configure do |config|
config.url = MY_CONFIG ['url']
config.application_name = MY_CONFIG ['app_name']
config.application_id = MY_CONFIG ['app_id']
config.logger = Rails.logger
config.log_level = :debug
# Rails config/initalizers/load_config.rb
# Custom config file loading automatically done via initializers
MY_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/my_config.yml")[Rails.env]
# config/my_config.yml
defaults: &defaults
url: http://url/to/service
app_name: my app
app_id: 1
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
end
Here's a simple project where you can see how you'd go by doing it: multiplier
First and foremost, if you're doing the gem management by yourself, please don't, use helper tools like jeweler to do it for you. Install the jeweler gem (gem install jeweler) and once you have it installed, create your gem projet:
jeweler --rspec your_gem_name
With this, it's going to setup a skeleton gem that's going to have a single main file (where you would require your necessary gem files) and the spec folder.
At the spec folder there's spec_helper.rb, that's where our configuration lives, what I did was:
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'multiplier'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
end
Multiplier.configure do |config| #these are the only lines I added myself
config.multiplier 4
end
So, here lives the config for our gem, but you could even do it on each spec, if you'd need it. But if you want to use a single config for all specs this is where you should place it.

Resources