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
Related
I use tutorial - https://github.com/galetahub/ckeditor/
And add cktext_area in _form
<%= f.label :body %>
<%= f.cktext_area :body %>
But it is not displayed
I use
gem 'carrierwave'
gem 'mini_magick'
In application.js require adding, routes modified, but not working at all, and use command
rails generate ckeditor:install --orm=active_record --backend=carrierwave.
Help!!!
assets.rb
# 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'
Rails.application.config.assets.precompile += %w( ckeditor/*)
# 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')
Rails.application.config.assets.precompile += %w[bootswatch.scss]
# Precompile additional assets.
# application.js, application.scss, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
I have a folder my_project/app/my_assets. Here's my config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join("app", "my_assets")
Dir.glob(Rails.root.join("app", "my_assets", "*")).each do |a|
Rails.application.config.assets.paths << a
Dir.glob(File.join(a, "*")).each do |b|
puts "b: #{b}"
Rails.application.config.assets.precompile << b
end
end
And here's code I have in application.html.erb:
<script src="<%= asset_path('my_js1.js') %>"></script>
That file is added to the pipeline, but I keep having this error:
Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( my_js1.js )`
to `config/initializers/assets.rb` and restart your server
I've restarted the server, but no avail.
I think your issue is that your b variable is an absolute path but precompile is looking for a relative path (relative to your assets path). I'll delete this if it doesn't work, but it's worth a try.
Try
relative_path = File.basename(b)
# or alternatively b.split("/")[-1]
Rails.application.config.assets.precompile << relative_path
I have a rails API with a mailer. In my mailer layout, I include an image :
<html>
<body>
<%= image_tag "quickbed_logo" %>
<%= yield %>
</body>
</html>
that is stored in app/assets/images.
However when I m sending the email I get :
Sprockets::Rails::Helper::AssetNotPrecompiled - Asset was not declared to be precompiled in production.
Add `Rails.application.config.assets.precompile += %w( quickbed_logo )` to `config/initializers/assets.rb` and restart your server:
I complied with the error message and added Rails.application.config.assets.precompile += %w( quickbed_logo ) to config/initializers/assets.rb
However I still get the error. What am I doing wrong ?
Add the extension to Rails.application.config.assets.precompile += %w( quickbed_logo.png ) because Rails cannot guess it.
Asset filtered out and will not be served: add
Rails.application.config.assets.precompile += %w( home.css ) to
config/initializers/assets.rb and restart your server
I am trying to setup multiple layouts that hit different .css.scss and .js setups, one for the home page, and others for various sections of the application.
My home layout looks like:
doctype html
= render 'layouts/components/view_source_msg'
html
head
title
= browser_title(yield(:title))
= render 'layouts/components/meta'
= render 'layouts/components/favicons'
= stylesheet_link_tag 'home'
= csrf_meta_tags
body[class="#{build_body_class} loading"]
== yield
= render 'layouts/components/analytics'
= javascript_include_tag 'home'
javascript:
$(function(){
$(document).foundation();
view_#{controller.controller_name.downcase}.init();
});
The error states to setup an initializer assets.rb which I have setup as:
Rails.application.config.assets.precompile += %w( *.css.sass )
Rails.application.config.assets.precompile += %w( *.css.scss )
Rails.application.config.assets.precompile += %w( *.js )
Rails.application.config.assets.precompile += %w( *.js.coffee )
Rails.application.config.assets.precompile += %w( *.js.coffee.erb )
But this is not working... Can anyone point me in the right direction? Thank you
Update
I was able to get this working by...
Rails.application.config.assets.precompile += %w( home.css )
Rails.application.config.assets.precompile += %w( home.js )
But this seems a bit off. In my old rails applications the other methods would have worked just fine...
Since Rails v4, the sprockets gem now handles the asset pipeline. It looks for stylesheet files inside the app/assets/stylesheets and the vendor/assets/stylesheets folders, so if you are putting home.css in public/assets/stylesheets or something, it's not going to look there. If you specifically tell rails to precompile any asset matching that name, like you did in your fix, it will do so and then output the compiled stylesheet and know to reference it, so that's why it works. However this is not the preferred convention. Ruby API for Coding Links to Assets
Asset filtered out and will not be served: add Rails.application.config.assets.precompile += %w( login.js ) to config/initializers/assets.rb and restart your server
I ge the above error when i try to run my application.
<%= javascript_include_tag "applicatin", "login" %>
I don't see any file name asests.rb in config/initializers.
You need to create config/initializers/assets.rb and add that line to it, like so:
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( login.js )
Then restart your server and it should work.The code above is all you need in the assets.rb file for now.