4

I'm following the first steps of the rails guide http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript

The index.html.erb looks like <a href="#" onclick="paintIt(this, '#990000')">Paint it red</a> <a href="#" onclick="paintIt(this, '#009900', '#FFFFFF')">Paint it green</a> <a href="#" onclick="paintIt(this, '#000099', '#FFFFFF')">Paint it blue</a>

I added coffeescript under app/assets/javascripts/welcome.js.coffee paintIt = (element, backgroundColor, textColor) -> element.style.backgroundColor = backgroundColor if textColor? element.style.color = textColor

I get this error:
Uncaught ReferenceError: paintIt is not defined

I tried chancing paintIt to @paintIt and window.paintIt to no avail. I have a temporary fix of just using plain old javascript in app/assets/javascripts/applications.js, but I wanted to begin using coffeescript. Any suggestions?

Abundance
  • 1,963
  • 3
  • 24
  • 46
  • 2
    You need to add `paintIt` to the global namespace. See here for good explanation on how to: http://stackoverflow.com/questions/4214731/coffeescript-global-variables. – vee Dec 19 '14 at 04:07

1 Answers1

1

You need to add paintIt to the global namespace. See here for good explanation on how to: stackoverflow.com/questions/4214731/

Via @vee. Copied here so this question can be closed.

Community
  • 1
  • 1
sam
  • 40,318
  • 2
  • 41
  • 37