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.
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.
Rails version: Rails 3.2.15
Ruby version: ruby 1.9.3p551 (2014-11-13 revision 48407)
Issue:
My rails app in development mode is serving files from storage folder which is at root level. The file format is pdf and mp4.
By serving i mean if I hit the route directly Eg: http://localhost:3000/assets/file_name.pdf is not throwing error and instead is opening on the browser.
I want to put these files behind authentication and hence tried putting it outside app/assets and public/ folders.
I'm not sure why is it able to serve any file outside those directories. Here are few of my configs for reference:
config.assets.enabled = true
config.assets.compress = false
config.assets.debug = true
Please help...
You must have a symlink within the assets directory, a setting in the pipeline, a monkeypatch in asset pipelines, or more likely, or the file itself is there's an 'assets' controller or route. Look carefully :)
Turns out there was some code which when you access the files from app/assets copied those to lib/assets as well. Hence, even after deleting the assets from app/assets it was serving a few files from lib/assets
2.2 Asset Organization
Pipeline assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.
That's the official documentation for order and organization of assets.
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)
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.
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.