2011-10-12 1 views
0

또는 각 필드를 개별적으로 위생해야합니까? 감사합니다레일 - 데이터베이스에 저장할 때 모든 필드에서 HTML을 제거하는 방법이 있습니까?

+0

개별적으로 하나를 수행하여 문제가 무엇입니까? 그게 무슨 뜻 이니? 스트링 콜렉션을 반복하면서 각 스트링에'strip_tags'를 적용해야한다면 문제가 될까요? – bdares

+1

나는 평균 15 개의 필드가있는 15 개의 모델을 말하게했다. 저는 모든 분야에 대해 초기 모델 수업에서 위생 처리를하고 싶지 않습니다. 나는 기본 설정을 sanatize로 유지하고 HTML을 지원하도록 허용하는 경우에만 필요합니다. 생각? – AnApprentice

답변

4

각 열을 반복하려면 열 메타 데이터를 사용할 수 있습니다. (http://blog.hulihanapplications.com/browse/view/10-strip-html-in-ruby-on-rails에서)

:

class Product < ActiveRecord::Base 

before_save :strip_html 
def strip_html # Automatically strips any tags from any string to text typed column 
    include ActionView::Helpers::SanitizeHelper 
    for column in Product.content_columns 
    if column.type == :string || column.type == :text # if the column is text-typed 
     if !self[column.name].nil? # strip html from string if it's not empty 
     self[column.name] = strip_tags(self[column.name]) 
     end 
    end 
    end 
end 
관련 문제