I've got some fonts added in the app/assets/fonts
they do not precompile for production ...
tried this in application.rb
module app
class Application < Rails::Application
config.assets.paths << Rails.root.join("app", "assets", "fonts", "tinymce")
whats going wrong?
I'm Using rails 4.2
Aside assets.paths you need to put assets.precompile in your application.rb:
For example:
config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
Related
I kept getting a 404 on my application, only to realize that it is looking for a font: /assets/fonts/footable.woff
So I figured I'd just create the fonts folder within my app/assets folder, and then re-run RAILS_ENV=production rake assets:precompile
I did this, and when I do an ls to public/assets/, there is still no fonts folder.
Here's my config/application.rb
module MyRailsApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# 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.
config.time_zone = "Central Time (US & Canada)"
config.active_record.default_timezone = :local # Or :utc
config.eager_load_paths += Dir["#{config.root}/lib/custom_rb/**/"]
config.assets.enabled = true
config.assets.paths << Rails.root.join("app", "assets", "images", "iCheck")
config.assets.paths << Rails.root.join("app", "assets", "fonts")
# For sidekiq
#config.active_job.queue_adapter = :delayed_job
config.active_job.queue_adapter = :sidekiq
end
end
and here's my config/initializers/assets.rb file:
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path.
# Rails.application.config.assets.paths << Emoji.images_path
# Add Yarn node_modules folder to the asset load path.
Rails.application.config.assets.paths << Rails.root.join('node_modules')
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
I have also restarted apache, and it's still not there.
What am I doing wrong?
I want to include all css files to precompile. Right now Im using #import to include other css into application.css. But I want to load specific css file according to the controller with:
<%= stylesheet_link_tag params[:controller] %>
It gave me an error, telling me to add the css to the precompiler. Say I add this to config/initializers/assets.rb:
Rails.application.config.assets.precompile += %w( pages.css )
Now it works, but only for that particular controller. I could just go manually add each css file for each controllers. It will work like that, but there must be a better way.
Im using bootstrap so I cant use:
*= require_
I have tried both (not at the same time):
Rails.application.config.assets.precompile += %w( *.css *.js)
Rails.application.config.assets.precompile = %w( *.css *.js)
but now I got this error:
Undefined variable: "$alert-padding-y".
Which I guess comes from bootstrap. But it was working before I try to add files to precompile.
=====
Edit: I tried what Daniel Westendorf post. Putting this code in the assets.rb:
Rails.application.config.assets.precompile = []
Dir[Rails.root.join("app", "assets", "**", "*.css")].each do |file|
Rails.application.config.assets.precompile << file
end
But I got this error:
Asset was not declared to be precompiled in production.
Add `Rails.application.config.assets.precompile += %w( application.css )` to `config/initializers/assets.rb` and restart your server
I tried to fix it by adding both the css and js (it asked for it later):
Rails.application.config.assets.precompile += %w( application.css application.js )
But I still ended up with the same error anyway:
Asset was not declared to be precompiled in production.
Add `Rails.application.config.assets.precompile += %w( pages.css )` to `config/initializers/assets.rb` and restart your server
This is generally against the better conventions of the web. One larger, cached, network request will perform better for you than many smaller, individual, network requests.
You could, however, find all the .css files in your assets directory, and add each one to the precompile array. Something like:
Rails.application.config.assets.precompile = []
Dir[Rails.root.join("app", "assets", "**", "*.css")].each do |file|
Rails.application.config.assets.precompile << file
end
I used this code. It works perfectly fine for me.
Add this code in config/initializers/assets.rb
# for css files
Dir[Rails.root.join("app", "assets", "**", "*.css")].each do |file|
Rails.application.config.assets.precompile << file
end
# for js files
Dir[Rails.root.join("app", "assets", "**", "*.js")].each do |file|
Rails.application.config.assets.precompile << file
end
When I configured my app files I put my plugin folders under assets and in my config/application.rb file I added the following lines:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Cnd
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
config.assets.paths << Rails.root.join("app", "assets", "fonts")
config.assets.paths << Rails.root.join("app", "assets", "images")
config.assets.paths << Rails.root.join("app", "assets", "bootstrap")
config.assets.paths << Rails.root.join("app", "assets", "rs-plugin-5")
config.assets.paths << Rails.root.join("app", "assets", "magnific-popup")
config.assets.paths << Rails.root.join("app", "assets", "owlcarousel2")
config.assets.paths << Rails.root.join("app", "assets", "morphext")
# 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.
end
end
Everything runs fine on my localhost when I deploy to an amazon ec2 I am getting the following error:
I do not understand why it loads the files locally but when I deploy it is not. I am just deploying in development environment for right now. Not sure if this makes a difference but I am working on my production environment locally so I precompiled assets. The reason I deployed in development is because I just need to show the client quickly the progress.
After some work I figured it out. I had to use this line RAILS_ENV=production bundle exec rake assets:clobber to get rid of the precompile and then everything went back to normal. Now when I am ready for production I need to figure out how to precompile all of the assets including the plugins.
I am studying rails and using the engines with rails.
In production mode, rails did not load compiled assets of engines,
although I have executed:
$ RAILS_ENV=production bundle exec rake assets:clean assets:precompile
Please help if anyone knows the issue.
My settings are as below:
environment/production.rb
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = false
config.assets.compile = false
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new`
engine/xxx/lib/xxx/engine.rb
Engines's option is -- mountable
module Moderna
class Engine < ::Rails::Engine
isolate_namespace xxx
# parent company asset precompile
initializer "xxx.assets.precompile" do |app|
app.config.assets.paths << Rails.root.join("app", "assets", "fonts")
app.config.assets.precompile << %w(
xxx/*.css xxx/fancybox/*.css xxx/skin/*.css xxx/google-code-prettify/*.css
xxx/*.js xxx/flexslider/*.js xxx/google-code-prettify/*.js
xxx/portfolio/*.js xxx/quicksand/*.js
xxx/*.svg xxx/*.eot xxx/*.woff xxx/*.ttf
xxx/customicon/*.svg xxx/customicon/*.eot xxx/customicon/*.woff xxx/customicon/*.ttf
)
app.config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
end
end
end
The rails engine getting started guide suggests including the following in your engine.rb
initializer "engine_name.assets.precompile" do |app|
app.config.assets.precompile += %w( admin.js admin.css )
end
It works for me.
I faced same issue with Rails 4 engine, I resolved by adding following code to engine.rb
Rails.application.config.assets.precompile += ['*.js', '*.css', '**/*.js', '**/*.css', '*.jpg',
'*.png', '*.ico', '*.gif', '*.woff2', '*.eot',
'*.woff', '*.ttf', '*.svg']
I need to precompile all assets so wild cards are used. You can specify files.
Second option is you can specify on Host applications
config/assets.rb
Rails.application.config.assets.precompile += %w( engine_name/file_name )
EDIT Just realised this question is a bit old. The below will work for you if you add an app/assets/config/my_component_manifest.js file to your engine and then use that to specify asset entry points.
You an also put it in the engine.rb file:
module MyComponent
class Engine < ::Rails::Engine
config.assets.precompile += %w( my_component_manifest.js )
end
end
I have a legacy application with disabled asset pipeline. I updated rails to 3.2.13, added :assets group in Gemfile, enabled assets in application.rb:
config.assets.enabled = true
config.assets.version = '1.0'
But assets won't load and compile before I explicitly set assets paths:
config.assets.paths << Rails.root.join("app", "assets", "stylesheets")
config.assets.paths << Rails.root.join("app", "assets", "javascripts")
config.assets.paths << Rails.root.join("app", "assets", "images")
I know that it must work without setting paths explicitly. But I just can't understand why it doesn't work now. It just works only when I define paths in my config files. Thanks.
The problem was in active_reload gem, it was included in Gemfile long time ago, and it is deprecated for Rails 3.2+. Removal of this gem fixed assets.