0

this is a way that i am passing subject line to a methods

@subject_text = html_subject(@customer_alert.alert.name)
@subject_text = html_sub(@customer_alert.alert.name)

and this are the two methods where i wanna replace all special character

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

and constant defined in model is

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

but first methods call all special character is replaced by null and second method call not returning any value

SSP
  • 2,650
  • 5
  • 31
  • 49
  • 1
    if my case & replaced my &amd; '"' rrplaced by " i wanna replace it with orignal sign – SSP Mar 30 '12 at 06:02

1 Answers1

0

Instead of this , you can use sanitize helper to remove special character.

Check the link : http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

Vik
  • 5,931
  • 3
  • 31
  • 38
  • 1
    if my case & replaced my &amd; '"' rrplaced by " i wanna replace it with orignal sign – SSP Mar 30 '12 at 06:03
  • where you want to display such output , I think you don't need to convert specific characters. Rails view helper will automatically convert into the desired output . use 'h', 'raw', 'html_safe' helper . For more info take a look http://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html – Vik Mar 30 '12 at 06:11
  • 1
    i used all this thing but its not working why i am not able to identify – SSP Mar 30 '12 at 06:23
  • 1
    Try this : CGI::unescapeHTML(str) – Vik Mar 30 '12 at 06:24