Abundance Abundance - 6 months ago 15
HTML Question

Coffeescript Uncaught Reference

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
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?

sam sam

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.