特殊角色被取代

时间:2012-03-30 05:38:40

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

这是我将主题行传递给方法

的一种方式
@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(/&lt;/,'<')
   end

   if s.gsub(/&quot;/,'"')

end
 s
   end
  end

和模型中定义的常量是

 SUBJECT_LINE = { '&amp;' => '&',  '&gt;' => '>',   '&lt;' => '<', '&quot;' => '"' }

但是第一个方法调用所有特殊字符都被替换为null 和第二个方法调用不返回任何值

1 个答案:

答案 0 :(得分:0)

除此之外,您还可以使用清理帮助程序删除特殊字符。

检查链接: http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

相关问题