Rails:rails 帮助程序中出现意外 tSTRING_BEG

Rails: Unexpected tSTRING_BEG in rails helpers

我正在尝试为我的 Rails 应用程序创建一个在我的视图中使用的辅助函数。

我正在使用 number_to_human ActionView::Helpers::NumberHelper 方法来格式化值。

这是我实现的功能:

def money(value, currency)
  return "N/A" unless value
  number_to_human(value, units: { thousand: 'K', million: 'M' }) "#{currency}"
end

我想要实现的是解析 currency 的值,它可以是 $ 或任何其他货币,具体取决于我想要什么。

但是,当我尝试测试时出现以下错误:

app/helpers/formatting_helpers.rb:18: syntax error, unexpected tSTRING_BEG, expecting keyword_end ...housand: 'K', million: 'M' }) "#{currency}" ... ^

如果我删除 "#{currency}",它工作正常。但是我需要一种方法将 currency 的值传递给函数。

请问我该如何解决这个问题。

您应该正确解析 return 值,例如:

def money(value, currency)
  return "N/A" unless value
  number = number_to_human(value, units: { thousand: 'K', million: 'M' }) 

  "#{number} #{currency}"
end