How to include CoffeeScript in Rails
13,459
If you have CoffeeScript enabled, Rails will generate a .js.coffee
file for each of your controllers in app/assets/javascripts
. That's where your CoffeeScript belongs, and you don't need to include script
tags.
Read more in the guide: http://guides.rubyonrails.org/asset_pipeline.html
Comments
-
rumblestrut almost 2 years
So CoffeeScript has shipped with Rails since 3.1? Fantastic! I'm getting into the syntax. I like simplicity.
And yet, I can't seem to figure out how to include it on my site. I've tried including it between and that didn't work.
I have one .erb file I want this to be in, new.html.erb (obviously, this is a Rails app):
<script type="text/coffeescript"> # Countdown to date script provided by JavaScriptKit.com # http://www.javascriptkit.com/script/script2/count.shtml montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") countdown(yr,m,d) -> today=new Date() todayy=today.getYear() if (todayy < 1000) todayy+=1900 todaym=today.getMonth() todayd=today.getDate() todaystring=montharray[todaym]+" "+todayd+", "+todayy futurestring=montharray[m-1]+" "+d+", "+yr difference=(Math.round((Date.parse(futurestring)-Date.parse(todaystring))/(24*60*60*1000))*1) if (difference==0) and document.write(current) elseif (difference>0) document.write("ONLY "+difference+" DAYS LEFT!") countdown(2012,4,30) </script>
And it doesn't show up at all. Ideas?
-
rumblestrut about 12 yearsAlrighty, but how would I call this in my new.html.erb file? It's a countdown script.
-
richardsun about 12 yearsYou would call the coffescript countdown function the same way you would call any javascript countdown function. Coffescript gets converted to javascript during compilation. You won't get very far if you haven't read and understood the Asset Pipeline guide.
-
shashi verma over 5 yearsbut now it only produces .coffee not .js.coffee