我在Rails 5.2中使用coffee脚本: 请参阅:http://guides.rubyonrails.org/working_with_javascript_in_rails.html
我的代码:welcome / index.html.erb:
<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>
welcome.coffee:
@paintIt = (element, backgroundColor, textColor) ->
element.style.backgroundColor = backgroundColor
if textColor?
element.style.color = textColor
$ ->
$("a[data-background-color]").click (e) ->
e.preventDefault()
backgroundColor = $(this).data("background-color")
textColor = $(this).data("text-color")
paintIt(this, backgroundColor, textColor)
但它不起作用并发生错误:未捕获的ReferenceError:$未定义
我的Gemfile已添加:
gem 'jquery-rails'
的application.js:
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
为什么会出现未捕获的ReferenceError:$未在rails 5.2中定义coffeescript?