我正在使用 gem prawn 生成 PDF。我想使用该 gem 创建表,所以我make_cell
将从该 gem 中使用,如下所示:
make_cell(
content: "#{cash_transaction[:transaction_date]}
\n#{cash_transaction[:creation_date]}
\n#{cash_transaction[:deletion_date]}"
)
问题是什么时候deletion_date(cash_transaction)
是 nil 它将打印 wecreation_date
和空行(来自\n#{cash_transaction[:deletion_date]}
. 如何以比下面更优雅的方式防止这种情况:
if cash_transaction[:deletion_date]
make_cell(
content: "#{cash_transaction[:transaction_date]}
\n#{cash_transaction[:creation_date]}
\n#{cash_transaction[:deletion_date]}"
)
else
make_cell(
content: "#{cash_transaction[:transaction_date]}
\n#{cash_transaction[:creation_date]}"
)
end
我建议你把字符串 buld 然后传递给 make_cell
content = "#{cash_transaction[:transaction_date]}
\n#{cash_transaction[:creation_date]}"
content << "\n#{cash_transaction[:deletion_date]} if cash_transaction[:deletion_date]
make_cell(content: content)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句