【问题标题】:Adding delimiter for best_in_place gem while editing在编辑时为 best_in_place gem 添加分隔符
【发布时间】:2014-09-16 01:00:24
【问题描述】:

这是THIS 线程的后续问题。 但是在我编辑该值并保存它之后,它又回到了一个非分隔的数字。 我怎样才能让它保持它的定界,所以即使在我编辑该字段后,它也会保持它的逗号?

所以在我最好的地方我有:

= best_in_place_if is_edit(@donation), @donation, :money_amount, display_as: :money_delimiter

在我的模型中:

class Donation < ActiveRecord::Base
  include ActionView::Helpers::NumberHelper
  def money_delimiter
    number_with_delimiter(self.money_amount)
  end
end

感谢大家的帮助。

【问题讨论】:

    标签: ruby-on-rails ruby best-in-place


    【解决方案1】:

    你可以使用虚拟属性,因为你已经有一半了:

    class Donation < ActiveRecord::Base
      include ActionView::Helpers::NumberHelper
      def money_delimiter
        number_with_delimiter(self.money_amount)
      end
    
      def money_delimiter=(value)
        self.money_amount = value && value.to_s.gsub(/[^\d\.]/,'').to_f
      end
    end
    

    然后在 best_in_place 中使用它:

    best_in_place_if is_edit(@donation), @donation, :money_delimiter
    

    我认为在有人编辑字段后,您必须使用事件来保证格式。使用 javascript 解析内容并确保其值。像这样的:

    $('.best_in_place[data-attribute=money_delimiter]').bind('ajax:success', function() {
      var content = $(this).html();
      // parse and check content
      $(this).html(content);
    });
    

    您可能必须使该选择器更具体。

    【讨论】:

    • 它说 .to_f 是未定义的。我应该在其中包含其他内容吗?
    • Doh。忘记 gsub 中的第二个字段:gsub(/[^\d\.]/,'')
    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 2013-05-24
    • 2012-05-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2012-02-24
    • 2012-12-16
    相关资源
    最近更新 更多