这是我将主题行传递给方法
的一种方式@subject_text = html_subject(@customer_alert.alert.name)
@subject_text = html_sub(@customer_alert.alert.name)
这是我想要替换所有特殊字符的两种方法
def html_subject(s)
s = s.to_s
if s.html_safe?
s
else
s.gsub(/[&><"]/) { |special| CustomerAlert::SUBJECT_LINE[special] }
end
end
def html_sub(s)
s = s.to_s
if s.html_safe?
s
else
if s.gsub(/&/,'&')
end
#{ |special| CustomerAlert::SUBJECT_LINE[special] }
if s.gsub(/>/,'>')
end
if s.gsub(/</,'<')
end
if s.gsub(/"/,'"')
end
s
end
end
和模型中定义的常量是
SUBJECT_LINE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
但是第一个方法调用所有特殊字符都被替换为null 和第二个方法调用不返回任何值
答案 0 :(得分:0)
除此之外,您还可以使用清理帮助程序删除特殊字符。
检查链接: http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html