Rails can't find assets if not explicitly set asset paths - ruby-on-rails

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.

Related

Rails won't precompile the assets-fonts folder

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?

Rails Assets Plugin Folders not being read in deployment

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.

Rails 4 - production images and fonts not pointing to fingerprint

I imported a custom theme into my Rails app for the home page. I've copied the entire folder into app/assets/customlibrary so that I don't have to split up the theme's files.
When I run my app in the production environment, Rails isn't able to find the fingerprinted assets in app/assets/customlibrary/* when they are referenced in
app/assets/customlibrary/css/style.css.
For example in style.css, background: url("../images/hero-image.jpg") results in GET http://localhost:3000/assets/images/hero-image.jpg 404 (Not Found)
I can't figure out how to fix this. Any advice?
folder structure
app/assets/customlibary/css/*
app/assets/customlibary/css/style.css
app/assets/customlibary/js/*
app/assets/customlibary/fonts/*.(svg|eot|woff|tff|)
app/assets/customlibary/images/*
app/assets/customlibary/images/hero-image.jpg
app/assets/stylesheets/themes/theme.css'
app/assets/stylesheets/style1.css'
app/assets/stylesheets/style2.css'
application.rb
config.active_support.escape_html_entities_in_json = true
config.filter_parameters += [:password]
config.encoding = "utf-8"
# setup bower components folder for lookup
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components')
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components', 'bootstrap-sass-official', 'assets', 'fonts')
config.assets.paths << Rails.root.join('app', 'assets', 'customlibrary')
# customlibrary assets
config.assets.precompile += %w( css/* fonts/* images/* js/* )
# normal stuff
config.assets.precompile << /\.(?:svg|eot|woff|ttf)$/
config.assets.precompile << /\.(?:png|jpg)$/
config.assets.precompile += %w( base.css )
config.assets.precompile += ['themes/theme.css']
config.assets.precompile += ['style1.css', style2.css', ... ]
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_assets = true
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :debug
Adding a path in config.assets.paths does not mean it makes the asset available from the web root of your application. (Which is my_app/public)
It adds a directory to the lookup path used by Sprockets and the asset helpers. When linking to assets from your stylesheets you can change the extension to .css.erb and use interpolation:
.class { background-image: url(<%= image_path 'image.png' %>) }
If you are using SASS, sass-rails maps the Rails asset helpers to SASS functions so you can just use:
.class { background-image: image-url("image.png") }
This does not require you to rename the file.

Rails fonts were not precompiled for production

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/

Rails - Loading assets through the Asset Pipeline

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.

Resources