Assets included manually in views not found - ruby-on-rails

If we include js/css files using application.css (like //=require_tree), then those files are working. But, I have stopped doing like that because it loads all js files of the project everytime.
So, I am adding (including) only required files per view basis. But they are not working when pushed to Heroku.

Including them on a per-view basis is not the right approach. First, you skip compilation (and thus you should store the assets in the public folder as static files), then you don't take advantage of the asset pipeline.
You can keep using the pipeline by splitting the assets in bundles and include only the bundle you want.
For example, you can remove the application.css file and split into alpha.css and beta.css, each file with its own includes. Add the files to the compilation in your production.rb file and you're done. Include those selectively, so that when you include alpha you will not load files included in beta.

Related

What is the proper way to link big template assets into rails erb files?

I am developing a rails application starting from webarch template. I know that adding the whole assets folder in the public/ folder will link the assets with my views, but it would not be taking advantage of the assets pipeline functions. The template has a lot of plugins and different options and one generally does not use all of it. The assets folder's size is 30MB.
I though about putting it inside vendor/assets and using it with the asset pipeline but this generates two problems:
I would be serving 30MB of minified code and using a small percentage of it in my app.
I would have to manually rewrite the whole assets folder to use links the way asset pipeline wants it (javascript_include_tag "file" to serve file.js). Of course, I would do this via a script but it still seems like a problem someone should have encountered first.
Since neither vendor/assets and public/ folders seem to be a proper location for these files I would like a better option (or a way to make the later options work better).
A solution to keep your files under asset pipeline when they are too big to reasonably be left in one single minimified asset file is to split your assets by categories, compile those categories in different minimified files, and include them in your views when needed.
I do it for an app that contains several "heavy" javascripts components that are located in different area of my app and are not often used.
1- Organize your file structure
In app/assets/javascrips and app/assets/stylesheets create one directory per category we are going to create. Examples:
app/assets/javascrips/common
app/assets/javascrips/admin
app/assets/javascrips/user_account
2- Create your manifests
In app/assets/javascrips and app/assets/stylesheets create one manifest file per category and have them included the related directory
File app/assets/javascrips/common.js
//= require jquery
//= require_tree ./common
File app/assets/javascrips/admin.js
//= require_tree ./admin
File app/assets/javascrips/user_account.js
//= require_tree ./user_account
3- Add your manifests to rails precompile list
You can do it in config/application.rb file, but when it gets big it is preferable to create an initializer file config/initializers/assets.rb
Rails.application.configure do
config.assets.precompile += %w[common.js admin.js user_account.js]
end
4- Include them in your views and layouts, and set-up your javascript libraries.
Import the assets files into layouts and views. It can be a good idea to create several layouts for different area of your application that would be using common assets files. The methods to use are
stylesheet_link_tag 'manifest_file' and javascript_include_tag 'manifest_file'
And keep in mind you may have to tell your javascript plug-ins they need to use the miniminied file when dynamically loading files. For them you can use a configuration .js.erb file. Example:
File app/assets/javascrips/admin/plug-in_config.js.erb
PLUGIN.config('dynamicFileName', '<%= javascript_path('manifest_file') %>');

How to point to existing web-app/js files with Asset Pipeline Plugin

We have a Grails app with loads of javascript files in the /web-app/js folder and we recently installed the Asset Pipeline Plugin... but just recently. The app is a few years old now.
We don't want to move all js files from /web-app/js folder to /grails-app/assets/javascripts folder since we might break some of the gsp files that are using those resources directly.
Let's suppose we have the file:
/web-app/js/myscript.js
We want to create a matching file in the assets folder...
/grails-app/assets/javascripts/myscript.js
... but that file should be empty except for one line like:
//=require /web-app/js/myscript.js
So the file in the assets folder only points to the real file in web-app folder.
The problem is that the above manifest line does not work.
The above might not make too much sense for just one file. But what we really want is to include several files (that already exist in /web-app/js folder) and use the Asset Pipeline Plugin to compile them all.
Is it possible?
Thanks a lot.
Unless the default changed in the more recent versions of the asset pipeline, just lose the /web-app prefix - the plugin searches both locations for compatibility reasons (think of all the plugins that aren't asset pipeline aware and would have to be modified..)
EDIT: in fact, looking at the Asset Pipeline documentation, you can loose /js as well since all folders under /web-app will be searched for the referenced file name.

How to access javascript files on Rails app when i did not add them to the precompile list?

I used a editor lib, that will load language related js files dynamically when you selected a language. In the development environment, that's fine, because assets is accessible, so the lib can access any files under assets. But in production environment, i did not add those files to precompile list, so those files are missing, and i also don't want to add them to precompile list.
So my question is whether there is a way to access javascript files even i did not add them to precompile listi on production app?
Any suggestion is welcome,thanks!
You could simply add them to the public folder, preferably in a js subfolder.

Precompiled assets and Non-compiled assets

Can I use assets that aren't pre-compiled along with assets that are inside a rails application?
I have separate CSS files uploaded through a web interface and don't want to precompile my assets everytime I add a new CSS file because a) that would be way too manual of a process and b) the styles all go to one page, like a theme, and so use the same class names. These CSS files do not require any processing as they are simple CSS files.
The CSS files are in a sub-folder of the assets folder inside the public folder: public/assets/styles/.
Is this possible?
Yes it is possible.
If i'm not wrong this SO link is very close to what you were trying to achieve
Rails assets:precompile only one asset?
Hope it helped !

Rails 3.1 asset pipeline - missing files from public/assets - why isn't this the default?

After I deployed my upgraded Rails 2.3.x -> 3.1 (rc4) app to our test environment this afternoon, all of our stylesheets and JavaScript files were returning 404 errors. We had added the rake assets:precompile task to our post-deploy script and it took a while to determine why the assets folder didn't have the pre-compiled files we expected.
In the end, the files weren't being compiled because apparently only application.css and application.js (+ non JS/CSS files) are processed by default.
We needed to change the following configuration value as follows:
config.assets.precompile += %w( *.js *.css )
Question: why isn't this the default?
I would have expected that anything that wasn't necessary to process as a manifest file would just get copied into public/assets. Much of what I've read on the asset pipeline is essentially "stick your assets in app/assets, configure the manifest files, and it should just work". Since the assets:precompile task didn't spit out any information about what it was doing, it took a while to determine that it just wasn't looking at the files we thought it would.
Is there any reason why this would not be a good value for the precompile configuration?
Thanks!
The idea is to have all your JavaScript and CSS always loaded in one shot, rather than loading bits and pieces as you move along. That way you always have the 'world' loaded and ready to work with, rather than having to include a whole bunch of individual files here and there.
It's a bit of a larger 'up front' load, but then the browser should keep loading all the javascript from cache. So the perceived speed of the site should speed up due to having everything cached and ready to go after the first request.
This was a controversial decision to include for Rails, but so is including CoffeeScript by default. Rails has always been an opinionated framework that way.
the new sprockets-based pipeline compiles all the files in /asssets/stylesheets and /assets/javascripts get compiled into application.css and application.js, respectively, by default.
In your views, you only need to link the application files, sprockets handles the rest.
Update:
Well, you don't have to make it all into just one file... You could have an shared.js, secure.js and public.js and have them each include the parts they need...
Think of them not as javascript files, but manifest files that establish groups of javascript files which you can then include as a group with a single javascript_include_tag. While the default is to include everything in the folder into a single file, you can be always pick and choose what to include, and what not.
The 'precompile' task simply runs those manifest files and compiles the multiple files into one, while pre-processing and sass or coffee script it runs across.

Resources