这是THIS主题的后续问题。 但是在我编辑了值并保存之后,它又回到了一个非分隔的数字。 我怎么能让它保持其划界所以即使在我编辑字段后,它会保留逗号?
所以在我的best_in_place上我有:
= 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
感谢各位帮助。
答案 0 :(得分: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);
});
您可能必须使该选择器更具体。