我正在尝试清理进来的属性作为一个字符串。我已经设置了一个模块,其中包含模型将在validate
上调用的多种方法。
类别:
class Preset < ActiveRecord::Base
include Shared::AttributeDataSanitizer
class AttributeError < StandardError ; end
validate :attribute_sanitization
def attribute_sanitization
raise AttributeError if sanitize_attributes(string).errors.length > 0
rescue AttributeError
errors.add(:string, "errored, some of your attributes are just terrible.")
false
end
end
模块:
module Shared::AttributeDataSanitizer
extend ActiveSupport::Concern
def sanitize_attributes(string)
errors = []
text = string.dup
extract(text).each do |attr|
if attr.validation != "valid"
errors << result
end
end
def extract(text)
calculation.scan(/(?<=\[).*?(?=\])/)
end
end
end
在extract
内调用sanitize_attributes
时,会发生以下错误:
[Rollbar] Reporting exception: undefined method `extract' for #<Preset:0x007f7dce91ba60>
[Rollbar]因为Rollbar被禁用而未报告异常
NoMethodError(未定义的方法extract' for #<Preset:0x007f7dce91ba60>):
app/models/concerns/shared/attribute_sanitizer.rb:8:in
sanitize_data_attributes&#39;
app / models / preset.rb:27:在attribute_sanitization'
app/controllers/presets_controller.rb:25:in
块中更新&#39;
app / controllers / presets_controller.rb:24:在`update&#39;
extract
不是私有方法,但这会引发错误,因为它认为它是一个类方法?这应该是一个实例方法吗?
注意:多个类需要使用它。