【问题标题】:Rails: Unexpected tSTRING_BEG in rails helpersRails:rails 助手中出现意外的 tSTRING_BEG
【发布时间】:2021-06-27 07:29:05
【问题描述】:

我正在尝试为我的 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: 语法错误,意外的 tSTRING_BEG,期待关键字_end ...housand: 'K',million: 'M' }) "#{currency}" ... ^

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

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

【问题讨论】:

    标签: ruby-on-rails helper


    【解决方案1】:

    你应该正确解析返回值,例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多