smit
smit

Reputation: 282

Ruby show html tag as string

I have got a Rails API which is displaying some data as JSON. The response looks good when a user enters plaintext. But when I add html tags or special characters like "" into the input, it converts them to unicode chars. I would like to render the entire thing as string, no matter what the user adds. I have tried using to_s but that still doesn't convert those bits into string.

 result.each do |row|
      i = row["i"].to_s
      j = row["j"].to_s
      obj[i][j] = {}
      obj[i][j]["name"] = row["name"].to_s
  end

Given: <p>name</p>
Output :\u003cp\u003ename\u003c/p\u003e
Expected output: <p>name</p>

Upvotes: 0

Views: 440

Answers (1)

Allison
Allison

Reputation: 2326

You are displaying data as JSON. That is what those chars look like as a JSON string.

'<p>name</p>'.to_json
=> "\"\\u003cp\\u003ename\\u003c/p\\u003e\""
JSON.parse "\"\\u003cp\\u003ename\\u003c/p\\u003e\""
=> "<p>name</p>"

Upvotes: 2

Related Questions