2013-04-23 152 views
0

我有這個在我的/assets/javascripts/leads.js.coffeeCoffeeScript的意外終止的錯誤

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文件中的函數。

請告訴我我哪裏錯了?

回答

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中的表達,並在你的函數的最後一個表達式,你不需要when後添加return

+0

哦謝謝。這工作。然而還有一件事,顏色不會改變,瀏覽器調試器會拋出一個錯誤:getRowColour undefined。你能幫我在這裏嗎? – 2013-04-23 04:54:37

+0

@sleeping_dragon,我在這裏回答正是問題 - http://stackoverflow.com/questions/6089992/cant-find-variable-error-with-rails-3-1-and-coffeescript/6090251#6090251讓我知道它是否有幫助! – Dogbert 2013-04-23 04:57:47

+0

這確實起作用。謝謝。但現在,AJAX不起作用。瀏覽器調試器不顯示任何錯誤,但它也不會更改行顏色! – 2013-04-23 05:07:18