为best_in_place gem添加分隔符

时间:2012-08-13 11:58:54

标签: ruby-on-rails

以正常形式我使用过:

<td>
   <%= number_with_precision(approved_invoice.invoice_amount,
      :precision =>2,
      :delimiter => ",")
   %>
</td>

这会在适当的位置添加逗号。如果我必须将此分隔符用于best_in_place gem:

,该怎么办?
<td class="tax-particulars-align">
   <%= best_in_place @invoice, :invoice_amount,
      :display_as => :get_invoice_amount_with_precision,
      :type => :input
   %>
</td>

如何在编辑页面中为金额字段添加逗号?有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您需要在模型的get_invoice_amount_with_precision方法中使用number_with_precision。为了使其可访问,您需要在模型中包含ActionView :: Helpers :: NumberHelper:

class MyModel < ActiveRecord::Base
  include ActionView::Helpers::NumberHelper

  def get_invoice_amount_with_precision
    number_with_precision(invoice_amount, :precision => 2, :delimiter => ',')
  end
end

答案 1 :(得分:0)

从最佳位置1.0.3开始,您可以在模型中使用自定义方法来决定如何显示某个字段。你可以写这样的东西(在视图中):

best_in_place @user, :description, :type => :textarea, :display_as => :my_formatting

在模型中:

class MyModel < ActiveRecord::Base

  include ActionView::Helpers::NumberHelper

  def my_formatting
    number_with_precision(description, :precision => 0, :delimiter => " ")
  end
end