jeudi 2 juillet 2015

Ruby gsub not replacing newline character

I have this code

str = @universal_claim_form.errors.full_messages.join
        str.gsub('Patient Contact Information: Value', 'Patient phone number') if str =~ /Patient Contact Information/
        debugger
        str.gsub("\\n", "<br/>")
        debugger
        flash.now[:error] = "Form has errors and was unable to be submitted.<br/> " << str

the first gsub replaces an unwanted message and the second gsub it meant to replace all the newline characters with html line breaks

at the first debugger line str = "PMP Validation failed. This happens when something you entered does not pass PMP specific validations.\\nPatient phone number: Value must be a 10 digit number"

and at the second debugger the line hasn't changed

what's even more weird is that I did this in irb at the command line and it worked

2.1.1 :001 > s = 'test'
 => "test"
2.1.1 :002 > s
 => "test"
2.1.1 :003 > s += '\ntest'
 => "test\\ntest"
2.1.1 :004 > s.gsub('\\n', '<br/>')
 => "test<br/>test"
2.1.1 :005 >

Aucun commentaire:

Enregistrer un commentaire