0

I have this in my /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'

and this in my /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 %> 

As can be seen my onchange function in f.select has a javascript which calls the function in my coffeescript file.

Please tell me where I am going wrong?

sleeping_dragon
  • 701
  • 1
  • 10
  • 27

1 Answers1

7

when and else statements need to be indented one level more than switch.

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'

Also, switch is an expression in CoffeeScript, and the last expression in your function, you don't need to add return after when.

Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • oh thanks. this worked. however there is another thing, the colour does not change and browser debugger throws an error: getRowColour undefined. can you please help me here? – sleeping_dragon Apr 23 '13 at 04:54
  • @sleeping_dragon, I've answered exactly that question here - http://stackoverflow.com/questions/6089992/cant-find-variable-error-with-rails-3-1-and-coffeescript/6090251#6090251 Let me know if it helps! – Dogbert Apr 23 '13 at 04:57
  • this does work. thanks. But now, the AJAX isn't working. The browser debugger does not show any error but it also does not change the row colour! – sleeping_dragon Apr 23 '13 at 05:07
  • @sleeping_dragon, hmm, you'd probably be better off asking another question, along with the relevant JS, generated html, etc. If you post a link here, I'll try to help if I can. – Dogbert Apr 23 '13 at 05:12
  • have asked a new question here: http://stackoverflow.com/questions/16161850/ruby-variable-in-javascript-in-rails-function – sleeping_dragon Apr 23 '13 at 05:46
  • my coffeescript global function didn't work on production (heroku). Says Application not defined. Any idea why? – sleeping_dragon Apr 23 '13 at 06:36
  • Fixed it. Had to run rake assets:precompile. Everything's working now. Thanks a lot. – sleeping_dragon Apr 23 '13 at 09:22