保存前按下多个属性

时间:2013-03-31 15:29:00

标签: ruby-on-rails

使用Rails。我在模型中有以下内容:

before_save :strip_tags

private

def strip_tags
  self.name = ActionController::Base.helpers.strip_tags(self.name)
  self.description = ActionController::Base.helpers.strip_tags(self.description)
end

除了将这些属性放在数组中并运行它们的循环外,还有什么更好的重构方法呢?

2 个答案:

答案 0 :(得分:0)

您可以尝试以下操作,在设置属性时(<1>}提供了更多行和更改,因此如果您需要html标记,这将无效你的验证)。

before_save

答案 1 :(得分:0)

您可以使用'attribute_normalizer'gem:

https://github.com/mdeering/attribute_normalizer

normalize_attributes :name, :description do |v|
  ActionController::Base.helpers.strip_tags(v)  
end