2

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?

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

3 Comments

Alrighty, but how would I call this in my new.html.erb file? It's a countdown script.
You 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.
but now it only produces .coffee not .js.coffee
-2

It is not considered a good practice but you have the option of evaluating coffescript on client side with js-base coffeescript compiler. Check the official guide.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.