Can anyone explain what the use of the following directories is?
app/assets/
lib/assets/
vendor/assets/
These directories are all a part of Rails' Asset Pipeline.
Conceptually, the app/assets directory is for your application assets (for instance, the stylesheets and images for your application). lib/assets is for all of the code that you've written that stands alone from your rails app (javascript library, maybe). vendor/assets is meant to house all third party libraries (e.g. jQuery).
All of these paths are included by default in the asset pipeline. This means that their contents can be included into other files using sprockets, concatenating automatically into one file (javascript or css), reducing the number of requests and thus the loading time. The asset pipeline can also compile your coffeescript and minify your javascript for production.
Related
Hello I am attempting to implement Trumbowyg emojis plugin in my ruby on rails application. So I have referred to the below link for guidance on how to implement the plugin.
https://github.com/Alex-D/Trumbowyg/blob/master/examples/plugins/emoji.html
$('#editor').trumbowyg({
btns: [['emoji']]
});
function initEmoji() {
emojify.setConfig({
img_dir : '../../bower_components/emojify.js/dist/images/basic/',
});
emojify.run();
}
$('.trumbowyg-editor').bind('input propertychange', function() {
initEmoji();
});
initEmoji();
How do I store the images in a directory and make reference to the directory under img_dir (as shown above) in a rails app?
Option 1: Embedding into Asset Pipeline
Without knowing exactly how this Javascript library works, the image path needs to be added to the Asset Pipeline. Otherwise rake assets:precompile will not include all of the emoji images in the manifest file it generates and will not know how to serve those assets in the production environment (assuming default Production environment configuration).
See #config/initializers/assets.rb
# Add additional assets to the asset load path
Rails.application.config.assets.paths << Rails.root.join('path', 'to', 'emoji', 'image', 'directory')
Option 2: Serve static images from /public
Another option is to not serve these files from the Asset Pipeline at all. It may be too much trouble to coerce the Asset Pipeline into serving these assets correctly without modifying the Javascript library to use asset-url in all the JS and CSS files.
Instead, copy the files to a directory under /public so the Rails app can serve them. Then adjust the img_dir in your Javascript configuration to be the path where the files are in /public. Ex: /emoji_images if you put the files in /public/emoji_images.
Depending on how you're hosting the app, you may have to configure serve_static_files in config/environments/production.rb.
I'm wondering what the best way is to use images & fonts from bower packages with middleman. As an example, I'm trying to add the slick.js carousel to my project. It's on bower and includes css, js, images, and fonts in the bower code.
With middleman, I have things set up where I've added the bower_components directory to the path for sprockets and compass, so the scss and js files are getting compiled correctly and working fine.
But the images and fonts aren't getting put anywhere where they'll be used. The slick.js library uses scss and is set up to use the compass image-url and font-url functions if they exist, meaning I need to somehow get the assets from the bower_components directory to be served from the same place as all my own images and fonts, and in a way that works in both the development middleman server mode as well as when running build.
How do I do this?
Obviously possible solutions are just to vendorize the slick.js library directly into my code or include it from the cdn where it's already hosted and not worry about not having it compiled into my single css and js files. Either could work fine but I'm wondering about the general case, surely this is common scenario for anyone using bower and middleman.
I figured it out - I thought compass was for requiring scss files and sprockets was just for the js, but middleman also uses sprockets (the middleman-sprockets library) for copying arbitrary static assets.
It's a bit manual and verbose (if there were a lot more files middleman suggests writing a script to auto-discover them by file extension types and import them) but my solution is to include the following in the config.rb file:
# set local vars I'll need to access later
images_dir = 'images'
set :images_dir, images_dir
# ... other config
sprockets.import_asset('slick-carousel/slick/ajax-loader.gif') {|p| "#{images_dir}/ajax-loader.gif"}
I use grunt, but it's the same issue. Generally you have the following options:
-Commit what you need in the bower_components directory right in to source control and reference your resources from there (somewhat recommended especially if something external is down when you are doing a build), or if you don't like exposing bower_components in URLs, create a route that directs to your bower_components folder
-Copy components on build/middleman script execution to a specified path. There will be no resources to check in for this option, you just choose a destination to reference in your code and have middleman copy your components out there.
The title pretty much says it all...
I tried adding /app/assets/fonts/font.woff and referencing it from my css file with /app/assets/fonts/font.woff but it doesn't seem to work.
Any ideas?
It turns out that the asset pipeline that #JimLim mentioned works a bit differently in Rails 4. Full docs here, but here's the relevant excerpt:
2 How to Use the Asset Pipeline
In previous versions of Rails, all
assets were located in subdirectories of public such as images,
javascripts and stylesheets. With the asset pipeline, the preferred
location for these assets is now the app/assets directory. Files in
this directory are served by the Sprockets middleware.
Assets can still be placed in the public hierarchy. Any assets under
public will be served as static files by the application or web
server. You should use app/assets for files that must undergo some
pre-processing before they are served.
In production, Rails precompiles these files to public/assets by
default. The precompiled copies are then served as static assets by
the web server. The files in app/assets are never served directly in
production.
So I ended up moving my /fonts directory into /public adjusting my paths in the #font-face declaration accordingly and everything works fine.
You have to tell Rails to include your fonts directory in the asset pipeline, as follows:
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
Finally, let Rails figure out the correct path for you, so you don't have to mess with the prefix app, app/assets etc. Add a .erb extension to your css/scss file e.g. application.css.erb, and use embedded ruby:
src: url("<%= asset_path('fonts.woff') %>");
(Related question)
In a Rails application - should the stylesheets and images be located under /public/ or under app/assets/?
You should put them in app/assets.
They then get minimised in production by the asset pipeline - see the rails guide for (lots) more information - http://guides.rubyonrails.org/asset_pipeline.html
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.
I have a bunch of static assets ( not jpg, css, & js) - rather files like pdf forms, xls that I need to serve to users. They rarely change. Before I used to store them in public folder, but with the introduction of the assets pipeline in rails 3.1 what is the best place to store files like that now now?
Actually I just tested it by creating a folder in app/assets/files and sticking my xls files in there, and rake assets:precompile task just picked it up.
Also this needs to be added for Rails < 3.1:
# Enable the asset pipeline
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/files"
The best place for items like this is still in the /public directory, remember to have your web server serve these assets directly also.
The assets directory is only needed if you want to take advantage of the asset pipeline. The asset pipeline handles things from compressing and compiling .coffee and .less or sass files to compressing your js and css into one file so your webserver only has to serve one file for each.
When you compile your assets with the rake task bundle exec rake assets:precompile they are moved to your public directory anyhow.