|UPDATE: Please see some of the forks for an updated version of this guide. I|
|myself have moved onto the Rails 3.1 betas to get the asset pipeline. But if|
|you want to stay on stable there are other folks who are keeping this guide|
|relevant despite the changes constantly occurring on Sprockets 2. The comments|
|on this gist will lead you to the right forks. :)|
|Some brief instructions on how to use Sprocket 2 in Rails to get CoffeeScript|
|powered JS and SASS powered CSS with YUI compression all via the magic of rack.|
|This stuff will be native in Rails 3.1 and the layout of the files on the|
|filesystem will be different but this guide will get you working with it|
|while we wait for all that to finalize.|
|Ignore the number prefixes on each file. This is just to ensure proper order in the Gist.|
|# Config a Sprockets::Environment to mount as a Rack end-point. I like to use a subclass|
|# as it allows the config to be easily reusable. Since I use the same instance for|
|# all mount points I make it a singleton class. I just add this as an initializer to my|
|# project since it is really just configuration.|
|class AssetServer < Sprockets::Environment|
|self.css_compressor = YUI::CssCompressor.new|
|Sprockets 2 has a lot more under the hood but this gets you started.|
|A few things not covered:|
|1. Anything supported by Tilt can be used as a template engine|
|(not just Sass and CoffeeScript).|
|2. Although Sass has native abilities to include other files, Sprockets 2|
|gives the ability to all formats through special comments like:|
|// =require "foo"|
|It's special commands can be fairly powerful (like requiring an entire|
|directory or tree). NOTE: Use the comment character relevant for the|
|language. So coffescript should be:|
|# =require 'foo.js'|
|Then you can create 'foo.js.coffee' and when served it will be as one|
|3. Sprockets 2 has the ability to pre-compile the assets for maximum speed.|
|Also useful when the deployment environment doesn't support a template|
|language (like CoffeeScript).|
@TrevorBurnham - You are correct. I was mistaken. I was thinking you could "require" a file but that is only for Node.js on the server side. So you will need to use Sprocket requires in your coffeescript. The comment character is not important. So put the following in your coffeescript:
Then create the file foo.js.coffee. When sent out to the client it is combined all into one file.
Useful to know. I have always done:
But I like your use of
Will have to try that on my next script to see if it works as I expect.
My Rails (3.0.3) on Ruby 1.8.7 p302 complains about
so I tried adding a
at the start of the 'def initialize' but that just made Rails complain about
what am I doing wrong?
well - I rather bluntly cp'ed your work <:)
all I attributed was renaming it (assets mean something entirely different to me) :) - and removed the 'asset' path in the initial join - but I did test that part - the join statement - in a console just to be sure and then I renamed the mounts in my routes.rb too, off cause ;)
I loaded/installed all the gem's mentioned and without the mounts, my rails is in pristine condition
It's great of you to get back to me so fast - been stuck here for far too long (quite a deroute from my early morning intention to 'just add coffescript' to my belt <:)
@ep-wac - I wrote this guide shortly before it was announced that Rails 3.1 would be using Sprockets. Ever since that announcement I have noticed a high level of activity on the Sprockets projects so it may be that they re-factored stuff rendering my guide obsolete (the price of bleeding edge). If you don't have time to mess with Sprockets I would suggest 'barista' as a more stable way of getting CoffeeScript in your application. If you have the time then you can of course dig into the Sprockets code and find out what they changed.
just a thought -
navigated to my sprockets gem in
and did a
and all he gave me back was
that does not look right? Does it?
I had problems getting this to work with
I've forked your gist here:
And done a sample Rails 3 app here:
@suranyami - Awesome to see you and other people keeping this gist relevant for Rails 3 despite all the changes affecting sprockets 2 that are constantly making this gist obsolete. Thanks for keeping this guide from going obsolete. I keep hoping Rails 3.1 hits soon so people can use the asset pipeline without rigging it themselves (or using beta software).
@suranyami, this is gold thanks! The Sprockets example of specifically mounting the Sprocket endpoint and then the Rails App endpoint didn't work for me in integration tests. This solved my issue on getting the JS loaded properly with capybara-webkit. Thanks so much!
@MohitSharma - Things have changed a lot in Sprockets 2 since I wrote this guide. You will likely need to change some things. Also some forks of this guide were made to accomidate changes in Sprockets 2. Unsure if those are still maintained or if their editor has moved onto Rails 3.1+. It should be possible to use Sprockets 2 in a Rails 3.0.11 app (since it is just rack based). You may just have to dig a bit.