Precompiled assets and Non-compiled assets - ruby-on-rails

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 !

Related

How to store different themes with assets?

I am hoping someone can explain how I can do the following with and without asset pipline in rails.
I have 3 themes that I need to use in a single rails application.
Each theme currently has the following structure:
/css/..
/fonts/..
/images/..
/js/..
I may add more themes in the future, so I would think it would be best to have each theme in its own folder, not spread around.
Can I use asset pipline with this theme requirement?
If not, how can I do it w/o using asset pipeline?
Firstly, I recommend you to use the asset pipeline as it improves the web application static assets loading performance by precompilation and setting caching headers for you.
For three themes viz. theme1, theme2, and theme3 this is what I'd do:-
Create individual layouts called theme1.html.erb, theme2.html.erb, and theme3.html.erb within the app/views/layouts directory.
Create stylesheets called theme1.scss, theme2.scss, and theme3.scss within the app/assets/stylesheets directory.
Create JS files called theme1.js, theme2.js, and theme3.js within the app/assets/javascripts directory.
Create subdirectories called theme1, theme2, and theme3 within the app/assets/images directory.
You can add the fonts directly into the stylesheets directory as they'll most probably be unique for every theme.
Reference the images using asset pipeline helpers as you'd do normally do after prepending the relative directory path for that theme in context to the app/assets/images directory before the image name.
Also, do not forget to import/require the files or add them to the asset compilation path within config/initializers/asset.rb file.

What is the best way of organizing and implementing page specific CSS and JavaScript in a Ruby on Rails application?

Question 1:
I've heard of creating div class' to cater to certain areas of the HTML but is there another way out there? Here's a link that I found related to what I mean.
http://brandonhilkert.com/blog/page-specific-javascript-in-rails/
Question 2:
Is there a way in order for me to organize my CSS and JavaScripts in my asset folder in the rails app IN SPECIFIC FOLDERS while accessing the asset pipeline benefits?
Eg.
Normal way:
assets
images(under assets)
stylesheets(under assets)
mycss1 CSS scss
mycss2 CSS scss
mycss3 CSS scss
javascripts(under assets)
myjava1 js
myjava2 js
myjava custom js
The Idea:
assets
images(under assets)
stylesheets(under assets)
myview(this is a subfolder of the stylesheets folder)
mycss1 CSS scss
mycss2 CSS scss
homepage(subfolder)
mycss3 CSS scss
javascripts(under assets)
myview(subfolder of javascripts)
myjava1 js
homepage(subfolder)
myjava2 js
myjava2 custom js
As far as I understand, you want to add your custom folders under the assets directory, put some files with specific CSS and JS and use them whenever you need it.
To achieve this you should include those files somewhere. By default, you have an appplication layout which includes application.js and application.css files which includes the rest of the related files. Those files known as manifest files and they are using sprockets gem to handle dependencies, preprocessing, compressing and other stuff (you can read more about it here). You can change the assets folder using config.assets.prefix or add new paths to look up for sprockets using config.assets.paths (This might be what you are looking for. Further reading here)

In Ruby on Rails, what is an "asset"?

What's considered an "asset" in the Ruby on Rails universe?
Are user generated files, such as images uploaded by the user, considered assets? Where should they be stored in the standard file structure of a Rails project? Should they be kept clear of any asset related directories like:
app/assets
lib/assets
public/assets
Relevant: The Asset Pipeline
Generally asset is anything that browser loads after it gets the HTML page. Meaning javascript, css and any images. But as you pointed out there are two different image types in a rails project.
1) the images related to your css and layout design, those go under the app/assets/images
2) the images that your users upload, those normally go into the public/system or public/uploads folder depending on what you use to receive the uploads
The lib/assets (or sometimes vendor/assets) is where you supposed to place js/css/images related to your front-end design and provided by third party libs. Say if you want to pull in some css or a js framework, that were you should place it.
And finally public/assets is where rails will compile your layout assets from the app/assets and vendor/assets folders when you run rake assets:precompile task for production.
To have it short, your design stuff goes to app/assets, and the user uploads go into public/system
User-uploaded files are not part of assets, and should definitely be kept clear of asset-related folders. You should put them somewhere in your public directory. I put mine in public/uploads, which is a common convention. And then you should ignore those files in git (or whatever VCS you're using).
Assets are basically: javascript, stylesheets, fonts, and images which are part of the site design itself, not part of the user-uploaded content.

Assets included manually in views not found

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.

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.

Resources