В моем приложении Rails у меня есть модель Post
.Rails 4: условно, если инструкция с coffeescript не работает
Я использую coffeescript, чтобы сделать форму сообщения более динамичной.
В posts.coffee
, у меня есть:
$(document).ready ->
text_max = 140
$('#character_count').html text_max + ' characters remaining'
$('#post_short_copy').keyup ->
text_length = $('#post_short_copy').val().length
text_remaining = text_max - text_length
$('#character_count').html text_remaining + ' characters remaining'
return
return
Это работает очень хорошо.
Теперь мне нужно вставить инструкцию if внутри этого кода, чтобы выполнить только $('#character_count').html text_max + ' characters remaining'
, если $('#post_short_copy').is(':empty')
.
Я попытался следующий код:
$(document).ready ->
text_max = 140
if $('#post_short_copy').is(':empty') {
$('#character_count').html text_max + ' characters remaining'
}
$('#post_short_copy').keyup ->
text_length = $('#post_short_copy').val().length
text_remaining = text_max - text_length
$('#character_count').html text_remaining + ' characters remaining'
return
return
но это возвращает ошибку:
SyntaxError: [stdin]:7:3: unexpected if
Любая идея, как правильно реализовать, если заявление в CoffeeScript?
Вы правы. Просто заменил вкладку двумя пробелами, и она сработала. Большое спасибо. –