Rails以视图格式显示结果

时间:2014-10-16 16:07:06

标签: ruby-on-rails view slim-lang

我需要在视图中显示结果
但格式不是很好 我的代码

      - @patient.weight.each do |m|
    tr
      td  = m.date
      td  = m.weight
      td  = m.weight  / (@patient.weight.first.inch * @patient.weight.first.inch).to_f

此结果在网页

02/09/2014  23.44   6.4930747922437675  

我只需要在第3栏,第6.49页中显示 需要帮助

2 个答案:

答案 0 :(得分:0)

您可以将round与包含所需小数的参数一起使用。

>> 6.4930747922437675.round(2)
=> 6.49

所以,只需将您的行改为

即可
td  = (m.weight  / (@patient.weight.first.inch * @patient.weight.first.inch).to_f).round(2)

这解决了我的问题,以避免视图中的错误

- @patient.weight.each do |m|
    tr
      - if m.date != nil
          td = m.date
      - else
          td = 'Sem pesos registrados'

      - if m.weight != nil
          td  = m.weight
      - else
          td  = 'sem peso registrado'

      - if @patient.weight.first.inch != nil
          td  = (m.weight / (@patient.weight.first.inch**2).to_f).round(2)
      - else
          td  = 'sem dados para este calculo'

BR

答案 1 :(得分:0)

简短形式

td = (m.weight / (@patient.weight.first.inch**2).to_f).round(2)

如果你总是希望精度为2,你也可以使用number_with_precision助手(Docs

td = number_with_precision(m.weight / (@patient.weight.first.inch**2).to_f, precision: 2)