Coffeescript意外终止错误

时间:2013-04-23 04:47:24

标签: ruby-on-rails ruby-on-rails-3 coffeescript

我的 /assets/javascripts/leads.js.coffee

jQuery ->
  getRowColour = (status) ->
    switch status
    when "rejected" then return '#FFA500'
    when "confirmed" then return '#C0C0C0'
    when "didn't connect" then return '#90EE90'
    else return '#FFFFFF'

这在我的 /views/leads/index.html.erb

<%= f.select(:status, ["to call","didn't connect","confirmed","rejected"], {:selected => lead.status}, :onchange => "$('#lead_form_#{lead.id}').submit();document.getElementById('lead_row_#{lead.id}').style.backgroundColor=getRowColour(#{lead.status});") %>
        <% end %> 

可以看出,f.select中的onchange函数有一个javascript,它调用我的coffeescript文件中的函数。

请告诉我哪里出错了?

1 个答案:

答案 0 :(得分:7)

whenelse语句需要缩进一级而不是切换。

jQuery ->
  getRowColour = (status) ->
    switch status
      when "rejected" then return '#FFA500'
      when "confirmed" then return '#C0C0C0'
      when "didn't connect" then return '#90EE90'
      else return '#FFFFFF'

此外,switch是CoffeeScript中的表达式,也是函数中的最后一个表达式,您无需在return之后添加when

相关问题