我以构建表单的下拉菜单来显示可用选项及其价格的列表,形式为“服务-价格”。但是,我的问题是,我无法在控制器中使用number_to_currency。是否有另一种方法可以达到相同的效果,或从控制器访问number_to_currency?这是我最初的努力:

@levels = []
DistributorLevel.all.each do |d|
  price = (d.price > 0) ? number_to_currency(d.price) : "Free"
  @levels << ["#{d.name} - #{price}", d.id]
end

最佳答案

是的,您可以这样做:

view_context.number_to_currency(d.price)


要么

ActionController::Base.helpers.number_to_currency(d.price)

关于ruby-on-rails - 可以在 Controller 内部使用“number_to_currency”吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23208856/

10-14 23:01