0

Example Case- How add filter to strip all html tags(this code not works, just psedocode for want i need to get):

class Person < ActiveRecord::Base
  validates :name, :presence => true
  #psedocode:
  **filters:name,:strip_tags=>true**
end
Ben
  • 25,389
  • 34
  • 109
  • 165

1 Answers1

3

Yes, ActiveRecord has a bunch of callbacks that you can tap into such as before_save, before_validation, etc. You can do something like this:

class Person < ActiveRecord::Base
  before_save :strip_tags

  private

  def strip_tags
    self.name = name.gsub(TAGS, '')
  end
end

More information in the Rails guide on ActiveRecord callbacks

Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • How you actually filter the name is up to you. If you have specific questions about filtering content, I would open a different question since you'll get better answers that way. – Peter Brown Aug 19 '12 at 17:14
  • (dry code problem)How can strip_tags be defined in one place for all models? – Ben Aug 19 '12 at 17:45
  • Put it in a module and include it in the models you want it in. – Peter Brown Aug 19 '12 at 19:27