Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 查看属性本身上的模型属性辅助对象_Ruby_Ruby On Rails 3.2 - Fatal编程技术网

Ruby 查看属性本身上的模型属性辅助对象

Ruby 查看属性本身上的模型属性辅助对象,ruby,ruby-on-rails-3.2,Ruby,Ruby On Rails 3.2,我的视图模型上有:含义和:读取属性。我想在将用户输入保存到数据库之前运行sanitize-before\u-validation来清除用户输入。现在,不要键入这样的内容: before_validation :sanitize_input def sanitize_input self.meaning = ActionController::Base.helpers.sanitize(self.meaning) self.reading = ActionController::Base

我的视图模型上有:含义和:读取属性。我想在将用户输入保存到数据库之前运行sanitize-before\u-validation来清除用户输入。现在,不要键入这样的内容:

before_validation :sanitize_input

def sanitize_input
  self.meaning = ActionController::Base.helpers.sanitize(self.meaning)
  self.reading = ActionController::Base.helpers.sanitize(self.reading)
end
def sanitize_input
  self.meaning = sanitize_attribute(self.meaning)
  self.reading = sanitize_attribute(self.reading)
end
def sanitize_attributes!(*args)
  args.each do |arg|
    arg.replace ActionController::Base.helpers.sanitize(arg)
  end
end
我想让它变得更好一点。所以我想出了一个ActiveRecordExtension:

module ActiveRecordExtension
  extend ActiveSupport::Concern

  def sanitize_attribute(attribute)
    ActionController::Base.helpers.sanitize(attribute)
  end
end

ActiveRecord::Base.send(:include, ActiveRecordExtension)
现在我可以像这样调用sanitize输入:

before_validation :sanitize_input

def sanitize_input
  self.meaning = ActionController::Base.helpers.sanitize(self.meaning)
  self.reading = ActionController::Base.helpers.sanitize(self.reading)
end
def sanitize_input
  self.meaning = sanitize_attribute(self.meaning)
  self.reading = sanitize_attribute(self.reading)
end
def sanitize_attributes!(*args)
  args.each do |arg|
    arg.replace ActionController::Base.helpers.sanitize(arg)
  end
end
我想通过在我的视图模型中执行类似这样的操作(类似于属性本身上的helper方法)来缩短这一点:

def sanitize_input
  self.meaning.sanitize_attribute!
  self.reading.sanitize_attribute!
end

但是无论我怎么做,我都无法在我的sanitize_属性方法中实现这一点(使用replace和bang(!)的各种组合)

通过使用这样的东西,它是否可以进一步缩短:

before_validation :sanitize_input

def sanitize_input
  self.meaning = ActionController::Base.helpers.sanitize(self.meaning)
  self.reading = ActionController::Base.helpers.sanitize(self.reading)
end
def sanitize_input
  self.meaning = sanitize_attribute(self.meaning)
  self.reading = sanitize_attribute(self.reading)
end
def sanitize_attributes!(*args)
  args.each do |arg|
    arg.replace ActionController::Base.helpers.sanitize(arg)
  end
end
用这样的话来称呼它:

sanitize_attributes!(self.meaning, self.reading)

最后一种方法对于需要清理多个属性的情况非常方便。是否可以按照我希望的方式进行清理?

这些输入来自哪里,您必须手动清理它们

试试这个:

def sanitize_attributes!(*attrs)
  attrs.each do |attr|
    dirty = self.send attr
    #this should mark the attribute as changed, so it's included with partial updates
    self.send "#{attr}=".to_sym, ActionController::Base.helpers.sanitize(dirty)
    #or
    #self.write_attribute(attr, ActionController::Base.helpers.sanitize(dirty))
  end
end

sanitize_attributes!(:meaning, :reading)

这些输入来自何处,您必须手动对其进行消毒

试试这个:

def sanitize_attributes!(*attrs)
  attrs.each do |attr|
    dirty = self.send attr
    #this should mark the attribute as changed, so it's included with partial updates
    self.send "#{attr}=".to_sym, ActionController::Base.helpers.sanitize(dirty)
    #or
    #self.write_attribute(attr, ActionController::Base.helpers.sanitize(dirty))
  end
end

sanitize_attributes!(:meaning, :reading)

你最终无法避免分配。在本例中,不必要的复杂性在我看来。在本例中,你最终无法避免分配。在我看来,不必要的复杂性在我看来。我只是喜欢提前处理类似的事情。我只是不相信用户在公共网站上的输入。PS你的解决方案工作得很好。谢谢!我只是喜欢在本例中处理类似的事情我只是不相信用户在公共网站上的输入。PS你的解决方案工作得很好。谢谢!