【问题标题】:Rails 4: conditional if statement with coffeescript not workingRails 4:带有咖啡脚本的条件if语句不起作用
【发布时间】:2016-03-06 01:46:16
【问题描述】:

在我的 Rails 应用程序中,我有一个 Post 模型。

我正在使用 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' if $('#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 中正确实现 if 语句吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 if-statement coffeescript conditional-statements


    【解决方案1】:

    这可能是一个缩进错误,类似于这个:

    Unexpected 'INDENT' in CoffeeScript Example Code

    考虑检查编辑器中的制表符和空格。

    【讨论】:

    • 你是对的。只需用两个空格替换选项卡即可。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2013-04-14
    • 2014-12-12
    • 2014-05-22
    相关资源
    最近更新 更多