C. Yee
C. Yee

Reputation: 197

Coffescript not compiling in rails app. Says syntax error - unexpected indentation

I'm new to coffeescript and I'm not sure what is wrong with my syntax. I want to add an error message if either the date or time field is empty upon clicking the update button. Here is my code.

$.add_error = (field, message) ->
  unless field.hasClass('input-error')
    field.after('<span class="input-error-message">' + message + '</span>')
  field.addClass('input-error')

$.remove_error = (field) ->
  field.removeClass('input-error')
  field.parent().find('.input-error-message').remove()

$('.btn.update_schedule').click ->
  date = $('#date')
  time = $('#time')
  if (date.val() && !time.val()) || (!date.val() && time.val())
    if !time.val()
      $.add_error(repeat_count, 'Please select both a date and time')
      false
    else !date.val()
      $.add_error(repeat_date, 'Please select both a date and time')
      false
    else
      $.remove_error(time) || $.remove_error(date)

I'm unable to compile this code because it says there's an unexpected indentation but I don't see it. Any advice would be greatly appreciated.

Upvotes: 0

Views: 33

Answers (1)

Luca T.
Luca T.

Reputation: 1651

In the last block $('.btn.update_schedule').click ->, there is an

 if ... else ... else ...

that is basically non-sense.

It seems that should be instead an

if ... else if ... else ...

This seems the correct version:

$('.btn.update_schedule').click ->
  date = $('#date')
  time = $('#time')
  if (date.val() && !time.val()) || (!date.val() && time.val())
    if !time.val()
      $.add_error(repeat_count, 'Please select both a date and time')
      false
    else if !date.val()
      $.add_error(repeat_date, 'Please select both a date and time')
      false
    else
      $.remove_error(time) || $.remove_error(date)

Upvotes: 2

Related Questions