[英]How to format interpolation within error message string
我的模型中有一个方法可以执行以下操作:
def price_estimate_range
if self.upper_price_estimate > self.lower_price_estimate + (lower_price_estimate * (1/10.0))
errors.add(:base, "Your upper price estimate must not exceed 10% of the lower price estimate. That is #{self.lower_price_estimate + (self.lower_price_estimate * (1/10.0))}")
end
end
如何设置插值格式: #{self.lower_price_estimate + (self.lower_price_estimate * (1/10.0))}
在字符串中使用number_to_percentage或类似的形式(在消息块中呈现时)?
<% @bid.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
def price_estimate_range
limit = self.lower_price_estimate + (lower_price_estimate * (1/10.0))
if self.upper_price_estimate > limit
text = number_with_precision(limit, precision: 2)
errors.add(:base, "Your upper price estimate must not exceed 10% of the lower price estimate. That is #{text}")
end
end
或sprintf限制,或类似的东西。 (如果您可能不得不多次使用该值,请计算一次并重用结果)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.