I have a rails engine with a built-in file uploader allowing users to upload files to the engine.
If in the application production config config.assets.compile = true then I can see the uploaded images from the engine within the application views.
If config.assets.compile = false then the images from the engine are not available, even after I run assets:precompile ...
in the engine.rb I have set the following:
initializer :assets do |config|
Rails.application.config.assets.paths << root.join("uploads", "my_engine")
end
any ideas?
thanks !!!
You need to add that directory to the precompile list. This extra line should get it for you
initializer :assets do |app|
app.config.assets.paths << Rails.root.join("uploads", "my_engine")
app.config.assets.precompile << Rails.root.join("uploads", "my_engine", "*")
end
I'm pretty lost, so I'm not completely sure what would be useful information for this issue, but I'm trying to serve some static assets for an angular app in rails, including as my own and third party JS, HTML, and CSS. I've created a directory called third_party under assets where all my bower installed components go, and a directory called my_app for my own client code.
I require the necessary code in a main_app.js file in app/assets/javascript:
//= require jquery
//= require jquery_ujs
//= require angular/angular
<snipped>
//= require_tree ../my_app/common
//= require main/index/index
//= require_tree ../my_app/main
I added those directories to the asset paths in my application.rb file with these lines:
config.assets.paths << Rails.root.join('app', 'assets', 'third_party')
config.assets.paths << Rails.root.join('app', 'assets', 'main_app')
And added them to the precompile step in assets.rb:
Rails.application.config.assets.precompile += %w( main_app.js )
Finally I have an assets being served via a custom assets_controller that looks like this:
class AssetsController < ApplicationController
skip_before_filter :verify_authenticity_token
def serve_main_asset
serve_asset_for_app("main")
end
protected
def serve_asset_for_app(app_name)
path = params[:path]
respond_to do |format|
format.html { render :file => "app/assets/my_app/#{app_name}/#{path}.html", layout:
false }
format.js { render :file => "#{path}.js" }
format.css { render(:file => "#{path}.css") }
format.json { render(:file => "app/assets/my_app/#{app_name}/#{path}.json") }
end
end
end
But whenever I try to GET any of these files, I'm seeing this error/stacktrace:
2016-08-04 01:23:16 -0500: Rack app error handling request { GET /assets/main/index/index.html }
#<NoMethodError: super: no superclass method `silence' for #<Logger:0x00000004bd78c0>>
/home/pawan/.rvm/gems/ruby-2.2.3/gems/activesupport-5.0.0/lib/active_support/logger.rb:63:in `block (3 levels) in broadcast'
...
On a possibly related note, I notice that the app is also trying to serve assets with a unique filename, even though I thought I have all cache-busting logic turned off for development mode. (ex: bootstrap.self-c8d853976ca268a5391a9470ebdcf3ba43e0b78936cdc925146afcb60a3116be.js)
I feel like the issue may just be an outdated gem of some sort? TIA.
I had the same issue.
The problem was I used a plain Ruby Logger (config/environmens/development.rb):
config.logger = Logger.new('/dev/null')
To make it work, I've changed it to use an ActiveSupport Logger:
config.logger = ActiveSupport::Logger.new('/dev/null')
This worked for me.
This is because of some recent changes in sprockets-rails gem, which is now offer a configuration option to silence asset request logging in development.
You can disable asset logging in your development.rb by setting below configuration.
config.assets.quiet = false
or use ActiveSupport::Logger instead.
config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
I am receiving this error when trying to compile assets on Heroku
Custom asset_path helper is not implemented
Extend your environment context with a custom method.
environment.context_class.class_eval do
def asset_path(path, options = {})
end
end
I have tried pretty much everything I could find through google. Heroku seems to be getting caught up when precompiling the assets active_admin.css, which contains 2 #import statements. If I comment those out, the build proceeds, but without the active admin stylesheets working in production.
If anyone has any thoughts, please advise. Thank you.
I fixed the issue by removing all config in application.rb relating to asset precompiling, and added a catch all compile script. I guess somehow it was missing a file, which caused the error:
# Precompile all assets
# application.rb
config.assets.precompile << Proc.new do |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
end
I am trying to set up asset pipeline on a older rails app that i have so that i can start using CoffeeScript. i am using ruby 1.9.3-p327 and Rails 3.2.13. I used to stash all my JS, CSS and my images in the public/ folder. This is what i have done so far ->
I moved them all to app/assets, i added the manifest files for both JS and CSS call //= require_tree ..
Added the following gems
group :assets do
gem 'coffee-rails'
gem 'uglifier'
gem 'sass-rails'
gem 'therubyracer'
end
Removed all my javascript_include_tags except for = javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'
Added the following to my config/application.rb file
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
config.active_record.whitelist_attributes = true
# 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'
Within the config/environments/development.rb file set
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
I added to the config/environments/production.rb
# Compress JavaScripts and CSS
config.assets.compress = true
# Choose the compressors to use
# config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :yui
# 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
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
I re-read the section on the Asset Pipeline just to see if i messed up anything. But so far none of the assets are pulling through from app/assets the only one that is working is the jquery.min.js that i have coming in through include_tag i have tried removing it and trying again no dice.
I have tried bundle exec rake assets:clean, and bundle exec rake assets:precompile, both of which run without issues. and have bundled since adding the gems for the assets, and restarted pow each time.
I am not sure if i am going through this all wrong or i have missed a step? any one gone through this before and tips or clues would be much appreciated.
You said you removed all the javascript_include_tag statements. Assuming you named the manifests app/assets/javascripts/application.js and app/assets/stylesheets/application.css, you'll need to include those in your layout:
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
Otherwise, Rails won't know to load your manifest files
Also, you shouldn't be using rake assets:precompile in development. If you do, then anytime you change a JS/CSS file, you'll have to recompile the assets before your changes show up.
I wish to precompile all the CSS and JS files in my project's app/assets folder. I do NOT want to precompile everything in vendor/assets or lib/assets, only the dependencies of my files as needed.
I tried the following wildcard setting, but it incorrectly precompiles everything. This results in lots of extra work and even causes a compilation failure when using bootstrap-sass.
config.assets.precompile += ['*.js', '*.css']
What is my best bet to only process my files in app/assets? Thanks!
config.assets.precompile = ['*.js', '*.css']
That will compile any JavaScript or CSS in your asset path, regardless of directory depth. Found via this answer.
A slight tweak to techpeace's answer:
config.assets.precompile = ['*.js', '*.css', '**/*.js', '**/*.css']
I would've added a comment to his answer but I don't have enough reputation yet. Give me an upvote and I'll be there!
NOTE: this will also precompile all the CSS/JavaScript included via rubygems.
This task is made more difficult by the fact that sprockets works with logical paths that do not include where the underlying, uncompiled resourced is located.
Suppose my project has the JS file "/app/assets/javascripts/foo/bar.js.coffee".
The sprockets compiler will first determine the output file extension, in this case ".js", and then the evaluate whether or not to compile the logical path "foo/bar.js". The uncompiled resource could be in "app/assets/javascripts", "vendor/assets/javascripts", "lib/assets/javascripts" or a gem, so there is no way to include/exclude a particular file based on the logical path alone.
To determine where the underlying resource is located, I believe it is necessary to ask the sprockets environment (available via the object Rails.application.assets) to resolve the real path of the resource given the logical path.
Here is the solution that I am using. I am fairly new to Ruby so this is not the most elegant code:
# In production.rb
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
}
With sprockets > 3.0, this will not work in production because Rails.application.assets will be nil (assuming default: config.assets.compile = false).
To workaround you replace the full_path assignment with:
#assets ||= Rails.application.assets || Sprockets::Railtie.build_environment(Rails.application)
full_path = #assets.resolve(path)
See also: https://github.com/rails/sprockets-rails/issues/237
I found this in the rails code:
#assets.precompile = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) },
/(?:\/|\\|\A)application\.(css|js)$/ ]
Which is backed up with the rails guide:
The default matcher for compiling files includes application.js,
application.css and all non-JS/CSS files
This default is not reset if you use +=, so you need to override it with a = instead of +=. Note that, apparently, you can pass a Proc or a regex to precompile as well as an extension. I believe, if you want to preompile only files in the top level directory, you will have to create a regex like:
config.assets.precompile = [ /\A[^\/\\]+\.(ccs|js)$/i ]
I'm revisiting this post at the year 2017.
Our product is still heavily using RoR, we've been continuously modifying our precompile configurations by appending Rails.application.config.assets.precompile as we're adding new modules. Recently I was trying to optimize this by adding a regex pattern, I found that the following glob pattern works:
Rails.application.config.assets.precompile += %w(**/bundle/*.js)
However I still need to exclude certain modules, so I tried hard to use regex instead of glob.
Until I looked into sprockets source code: https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L108 , I found that they're already using regex:
app.config.assets.precompile +=
[LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
So I change my code into:
Rails.application.config.assets.precompile +=
[/^((?!my_excluded_module)\w)*\/bundle\/\w*\.js$/]
Which works well.
This will get all .css .scss and .js including all files in subdirectories.
js_prefix = 'app/assets/javascripts/'
style_prefix = 'app/assets/stylesheets/'
javascripts = Dir["#{js_prefix}**/*.js"].map { |x| x.gsub(js_prefix, '') }
css = Dir["#{style_prefix}**/*.css"].map { |x| x.gsub(style_prefix, '') }
scss = Dir["#{style_prefix}**/*.scss"].map { |x| x.gsub(style_prefix, '') }
Rails.application.config.assets.precompile = (javascripts + css + scss)
I wanted all assets from both /app and /vendor to be compiled, except partials (which name starts from underscore _). So here is my version of an entry in application.rb:
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
vendor_assets_path = Rails.root.join('vendor', 'assets').to_path
if ((full_path.starts_with? app_assets_path) || (full_path.starts_with? vendor_assets_path)) && (!path.starts_with? '_')
puts "\t" + full_path.slice(Rails.root.to_path.size..-1)
true
else
false
end
else
false
end
}
Additionally it outputs list of files being compiled for debugging purposes...
This snippet includes all js/css files, excluding gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass"). It also has a strategy for including assets from Gems which aren't included in every page.
# These assets are from Gems which aren't included in every page.
# So they must be included here
# instead of in the application.js and css manifests.
config.assets.precompile += %w(a-gem.css a-gem.js b-gem.js)
# This block includes all js/css files, excluding gems,
# under: app/assets, vendor/assets, lib/assets
# unless they are partial files (e.g. "_file.sass")
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
aps = %w( /app/assets /vendor/assets /lib/assets )
if aps.any? {|ap| full_path.starts_with?("#{Rails.root}#{ap}")} &&
!path.starts_with?('_')
puts "\tIncluding: " + full_path.slice(Rails.root.to_path.size..-1)
true
else
puts "\tExcluding: " + full_path
false
end
else
false
end
}
Although, you probably wouldn't want to do this since you'd likely be precompiling gem assets twice (basically anything that is already \=require'd in your application.js or css). This snippet includes all js/css files, including gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass")
# This block includes all js/css files, including gems,
# under: app/assets, vendor/assets, lib/assets
# and excluding partial files (e.g. "_file.sass")
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
asset_paths = %w( app/assets vendor/assets lib/assets)
if (asset_paths.any? {|ap| full_path.include? ap}) &&
!path.starts_with?('_')
puts "\tIncluding: " + full_path
true
else
puts "\tExcluding: " + full_path
false
end
else
false
end
}