How can I drive Rails asset pipeline from a controller? - ruby-on-rails

In Rails, I want to drive the asset pipeline to compile and minify my js file and get the result as a string. The js file uses sprockets to combine multiple files together.
How can I do this?
This needs to be run from a controller action. It's not as simple as just serving an asset, rather, I want to get my compiled and minified JS as a string and then do something with it.

You can do something like this to access your asset.
view_context.asset_path 'your_file.js'
Check this question also
How does one reference compiled assets from the controller in Rails 3.1?

Related

how to have assets other than js and css files in rails

I have some object files which I want to have them in my JS codes I put them under app/assets/3d-models and when I try to get a link to it I use:
<%= asset_path("3d-models/splits2/xxx.obj") %>
But the output is /3d-models/splits2/xxx.obj which obviously the 404 NOT FOUND is the result(i.e the asset not found, the wrong link!)
Question:
How can I get an access link to a file other than common files used in rails' assets?
You can have different assets. Just place them into assets folder and link to them correctly.
Rails are using assets pipeline. This is a concept, which provides a better way to serve some static files to a website. In Rails it is implemented by Sprockets gem. Please read more here.
You can also have other static files directly in public folder and link to those files anywhere from your app. This will not be included in assets pipeline.
If you use assets_path helper method, then there is a fingerprint added to a file so the URL to the file is different then.
Please check this section.

Rails 4 Heroku Assets not loading, though in heroku asset pipeline

I have a problem with certain assets on heroku. (local environment is working fine)
The assets are in the pipeline. If I execute in the heroku rails console:
helper.asset_path("typicons.woff")
helper.asset_path("backgrounds/1.jpg")
I get the following response:
/assets/typicons-c2430aad2b6a33948dc064cfaee8ad65ff9e3ca439834f3aaa84abec3d10dea8.woff
/assets/backgrounds/1-c2098ff7e7fbb89b2d18e9cd9089f712f2b837265d1d2e4182c36c23392760c6.jpg
So I assume that the assets are in the heroku asset pipeline. As well by opening the url directly with the digest in it, I receive the file.
However if I try to reference the files in css or javascript like this:
$('.top-content').backstretch("/assets/backgrounds/1.jpg");
The file does not load. As well opening /assets/backgrounds/1.jpg directly does not work. Referencing assets from .rb or .erb files works.
Please can someone tell me, what kind of config I have to change, so the URLs for assets work as well without the digest?
Thank you!
Assuming you are using a fairly standard asset pipeline setup, this passage from the Rails Guides should help:
If you add an erb extension to a JavaScript asset, making it something such as application.js.erb, you can then use the asset_path helper in your JavaScript code:
-- http://guides.rubyonrails.org/asset_pipeline.html (section 2.3.3)
In your example, add the erb extension to your JS file and then change the line to
$('.top-content').backstretch(<%= asset_path("backgrounds/1.jpg") %>);
The problem is that Rails 4 does not support non-digested assets at all.(For whatever reason)
Here is a more thorough explanation on the issue: Non Digested Asset Names in Rails 4
My personal workaround was to add a public asset folder to my app:
public/assets/static/
and upload the assets required there. Since it was only about fonts and background images, which do not change often, it does not seem to be a problem. In the link above there are several other solutions proposed.

Grunt and Rails

I'm working on using a Grunt workflow to manage my assets in my Rails app rather than Sprockets.
So far, I have my apps JS and CSS both being concatenated and minified into public/assets/javascripts/application.js and public/assets/stylesheets/application.css respectively.
And also have my Bower components JS and CSS being concatenated and minified into public/assets/javascripts/vendor.js and public/assets/stylesheets/vendor.css respectively.
Fonts and Images from Bower components are then copied into public/assets/(images|fonts).
This is all well and good but now I need the references to fonts/images within those files to be updated to reflect their new location.
I have looked at cssmin and yes it rewrites file references but I cannot get the file path to change depending upon the type of file being referenced.
Any ideas on how I can do this?
Also, I ahve been reading about Grunt plugins which can read your view files and use those to minify and concatenate files and update the and tags in the views for you.
Surely I can't do that in a Rails app? Is there a way I can deal with this in Rails?
This other StackOverflow post may be of help:
Integrate Grunt into Rails asset pipeline
The accepted answer recommends using the Half Pipe gem.
The second answer linked to a blog post about a Do-It Yourself solution: Goodbye, Sprockets! A Grunt-based Rails Asset Pipeline.
I haven't used either solution, but they are worth a try.

Rails: Exclude certain assets from being precompiled

What I want is really simple - I want all assets except a particular css file (that references image assets and thus needs to go through the asset pipeline) to be precompiled. How do I exclude that css file from being precompiled?
There are a couple possible answers depending on what you want. I'm not quite sure what you're asking.
If it's in app/assets, you really need anything in there to go through the asset pipeline to be available. In production, you need to precompile all your assets in that are part of the asset pipeline -- and need to re-precompile in production mode whenever they change.
If the asset name doesn't have a scss or sass suffix, it won't go through the scss/sass compiler, although it'll still go through the asset pipeline for fingerprinting in filename, gzipping, etc.
If you really don't want an asset to go through the asset pipeline at all, then you can't really put it in app/assets. You have to put it in public instead. Then it gets confusing how to generate a URL to it, the best way is to use public_path + "/subdir/foo.css".
But I'm not sure that's what you're asking. If you have an asset file that references image paths, normally you do need that to through the asset pipeline, absolutely! Because it's the asset pipeline that will fill out the URLs properly for those image assets. You'll want to use the Rails asset_path helper in the asset file to generate the URL to an image. You can put an .erb on the end of the file, and then use ERB codes <%= asset_path('my_asset.css') %> in it it. Or if it's sass,asset_path` might be built into rails sass already, I forget.
I don't fully understand what problem you are having exactly, but I think what you are asking for may not actually be what you need. Have you checked out the Rails Asset Pipeline guide? It's pretty decent. http://guides.rubyonrails.org/asset_pipeline.html
It sounds like you actually want a file precompiled separately from the other files?
If so you just need to add it to the Rails.application.config.assets.precompile array. Look in config/initializers/assets.rb for a commented line as an example.

Rails 3.1 Asset Pipeline - Why should I use the Asset Helpers in a SCSS file?

I'm just getting into the Asset Pipeline; I'm using SASS/SCSS, but I'm not understanding why I should be using the Asset Helpers.
For example, if I have some CSS/SCSS without using an Asset Helper:
background-image: url('rails.png');
This will work fine because both my .SCSS file and image are in and accessible through the assets directory.
What's the point of doing this?:
background-image: asset-url("rails.png", image);
I understand it will add "/assets/" to the url, but why should I be using the Asset Helpers if the standard CSS way will work?
I think I'm missing something. Does it have something to do with deploying to production?
Using the helpers gives you access to the finger printed URLs in production. From the Asset Pipeline guide:
In the production environment Rails uses the fingerprinting scheme outlined above. By default it is assumed that assets have been precompiled and will be served as static assets by your web server.
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name.
So in production, the paths have an MD5 appended and you have things like this:
/assets/pancakes-af27b6a414e6da00003503148be9b409.png
With the checksums in place, Rails can tell browsers to cache these files forever. Then, if you do a new release that changes one of your assets, the checksum changes and that changes the whole path; the new path makes the browser think it is a whole new file so it will fetch it again. Without the checksums you can easily get old files stuck in browser caches and that sort of thing isn't exactly a happy fun time.

Resources