rails 4 asset pipeline vendor assets images are not being precompiled - asset-pipeline

I'm using rails 4 & ruby 1.9.3 for my application and fancybox2-rails gem, but there's a general problem with asset pipeline. If I run rake task for precompile, then everything is fine except for images in vendor/assets/images and ../gems/ruby-1.9.3-p327/gems/fancybox2-rails-0.2.1/vendor/assets/images. Images from these two folders are not being precompiled and eventually I have a problem with dead links to non-existing images. Any suggestions?

It seems like images are included by default only from app/assets folder. So the solution is to add this line to config/application.rb
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)

It sounds Sporker can't autoload images from vendor/assets/images.
2.2 Asset Organization
Pipeline assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.
app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.
lib/assets is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.
vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks
From the description come from guides.rubyonrails.org, I don't think they ignored vendor/assets/images with no intention.
So I just added the follow line:
#config/application.rb
config.assets.paths << Rails.root.join("vendor", "assets", "images")
And, I solved my problem.
I hope this will work for you.

For my Rails 4.2.x project, I just moved the vendor images to vendor/assets/images and added this to application.rb:
config.assets.precompile += %w(vendor/assets/images/*)
Works fine on development and production.

Related

How to handle Bower components images, fonts or other assets (with yeoman)?

I'm building a webapp using yeoman. Everything is great except the fact that I'm getting a bit confused with using assets (like images or webfonts) that are bundled with some bower components (sass-bootstrap for example...)
I include javascript assets using Requirejs, but my main problem lies with the images and fonts... What's the right approach? Can't seem to find it anywhere... Should I copy all the font files to the font directory I use with compass? Those files wouldn't be updated when the bower component is updated...
Any help would be welcome!
Don't copy, use your bower_components directory as another assets directory like scripts or styles. Link to it in your requirejs paths, and link to it in your compass config.
If you want, during your build you could look into your bower folders and copy all images and/or fonts into other subdirectories in your dist location.
You made a good point, copying source files won't get updated when you update. That's why you want to try as hard as you can to just leave them as is.

How can I stop Padrino putting compiled SASS in my public/ directory? Or should I?

I'm playing with Padrino, experimenting with a very minimal site at the moment with no DB and just a few HAML and SASS files under the app/ directory.
What I've noticed is that each time I render a page which links to a stylesheet that's defined in a .sass file, it compiles the stylesheet to .css and stores it under public/.
This is all very well, but I also have some static assets stored in public/, including images and some other hand-written .css files.
What this means is that my public/ directory becomes a mix of things I placed there and things compiled by Padrino. So, looking in there will show a bunch of .css files, some of which are compiled .sass files, and some of which are my actual primary static assets. This is confusing.
Is there a way I can stop Padrino (or Sinatra, or Rack, or whatever is doing it) from saving these compiled files during development, and keep my public/ clean?
Alternatively, can someone explain why what I'm asking for is a bad idea / by design / I should learn to love it instead? :-)
Thanks.
I don't know how to set the SASS settings for Padrino, I had a look and couldn't find anything helpful either. I would feel a bit nervous about running it this way too, it could easily get confusing and unhelpful, and what if the asset names clash?
What you could do is not add SASS in via Padrino, and then run it yourself either via the --watch switch or via something like Guard. That way you can also specify different subfolders within the public directory (images/css/js etc), which is what I do (although it does mean you need to remember to add the subfolder as part of the path when describing links). The app doesn't even need to know you're using SASS, and precompilation, when it's this simple, is surely better than the kind of compilation on demand that you've got at the moment (IMO).
You might try the Padrino mailing list for help with the settings.
Using the padrino-sprockets gems I also wanted to change the default /public/stylesheets directory to /assets/stylesheets where sprockets pick them up. I found that my padrino project genereated with the -c sass option had a /lib/sass_plugin.rb file with the following:
# Enables support for SASS template reloading for rack.
# Store SASS files by default within 'app/stylesheets/sass'
# See http://nex-3.com/posts/88-sass-supports-rack for more details.
module SassInitializer
def self.registered(app)
require 'sass/plugin/rack'
Sass::Plugin.options[:template_location] = File.join(Padrino.root, "app/stylesheets")
Sass::Plugin.options[:css_location] = File.join(Padrino.root, "public/stylesheets")
app.use Sass::Plugin::Rack
end
end
Editing the :css_location path and restarting Padrino did the trick!

How can I get rails to not precompile assets in vendor directory?

asset:precompiling is taking forever on my app. I pretty much never send assets in the vendor directory directly to user and so I don't need those to be precompiled. They are always included by another file in the app/assets directory.
How can I get the precompiler to ignore assets in vendor/assets?
You may want to make sure that the config.assets.precompile statement in your application.rb is only referencing the specific files that you want to precompile. If an asset (js or css) isn't listed in this statement, it won't be precompiled by rake assets:precompile. For example:
config.assets.precompile += ['public/public.js', 'public/public.css', 'admin/admin.js', 'admin/admin.css']

Files added to config.assets.precompile are not served in development

I am using the activeadmin gem which proposes a little weird way of including additional assets to those automatically generated by the gem:
https://github.com/gregbell/active_admin/issues/492
My problem however is that though in config/application.rb I do set
config.assets.precompile += ['active_admin.css', 'active_admin.js', 'my.css', 'my.js']
my_file.css and my_file.js are not served in development mode, it's Routing error.
I have registered them with the activeadmin's own config.require_javascript and config.require_stylesheet, which seems to work, since generated html contains the respective script tags.
However the /assets/my.css and /assets/my.css are not responsive.
What could cause a problem, how do you think?
It turned out that the files I was trying to add were corrupted somehow since creating the new ones and copying the content inside made a trick.
I was including the .js and .css files of this plugin:
http://loopj.com/jquery-tokeninput/
which is fine not taking this issue into account.

Asset precompile missing standalone javascript / css

I have that sneaking feeling I'm missing something obvious:
I ran
RAILS_ENV=production bundle exec rake assets:precompile
to precompile my assets before pushing to Heroku. Looking in /public/assets shows that application.js and application.css manifests successfully compiled, but none of my standalone files precompiled:
admin.js.coffee
blog.js.coffee.erb
[ ... several more similarly named ... ]
twitter.js.coffee.erb
and
admin.css.less
home.css.less
public.css.less
are all missing from /public/assets.
I thought that Rails would precompile the application.js/.css files, plus anything else that doesn't end in js/css:
The default matcher for compiling files includes application.js,
application.css and all files that do not end in js or css:
[ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
from: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
I don't want to have to manually update config.assets.precompile every time an asset file name changes. Am I missing something that will make Rails precompile these assets?
Update
Based on #Richard Hulse's answer below, I tested it out by created a separate manifest file for a standalone asset (i.e. I renamed twitter.js.coffee.erb to twitter-include.js.coffee.erb and added twitter.js with a single //= require pulling in the renamed original). This seems to work.
There must be a cleaner way than this, and it seems to contradict the Rails guide quoted above. The guide says the only files that won't be compiled are .js or .css files not named application. What I'm see is only .js or .css being directly compiled (i.e. not via a manifest) - nothing else.
Two things:
If these files are included in your application manifests, then they are included in the site's application files.
There should be a line in both application manifests: require_tree, that will pick up all the assets automatically for your. Is that in these files?
Edit in reply to edit:
The way I would structure this is have two sets of manifests. The standard ones (application.css/.js) are for public. The admin set are for admin pages. Include all the stuff you want in admin.js/.css manifests and add those files to the precompile array:
config.assets.precompile += ['admin.js', 'admin.css']
This would allow you to share code between the two groups. For example you can include jquery in both, but jquery_ujs in admin only. In your admin section layout just include the admin manifests instead of the application manifests.
In practice you will then add new files to the application or admin manifests as you develop the site, and you won't have to change the precompile configuration.
Once you get to the point of adding lots of assets, an admin section and so on, it is expected that things will get more complex and that you have to be explicit about what is included in manifests and the order (as opposed to require_tree).

Resources