开发者

Escape characters <%= h("") %> and how to deal with double quote in Ruby on Rails

开发者 https://www.devze.com 2023-01-09 05:51 出处:网络
This might be a simple question to answer, but I couldn\'t find the answer. In Ruby on Rails, I thought the helper function would help escape special characters.

This might be a simple question to answer, but I couldn't find the answer.

In Ruby on Rails, I thought the helper function would help escape special characters.

Example: " She's the one that took me "to" "

Code wise: <%= h("She's the one that took me "to" ") %>

However, the double quote won't allow me to display the code on the browser and gives me an error.

I thought the h() is an alias for html_escape() and they convert the following 4 characters

 <   >    &    "

into

&lt;   &gt;  &开发者_如何转开发amp;amp;  &quot;

Is there something i'm missing with using double quotes?

Any advice is appreciated Thanks, D


The problem is that your double quote around the word to is closing the double quote opened at the beginning of the string. Try this:

<%= h("She's the one that took me \"to\" ") %>

Or, to avoid having to backslashify the internal double quotes, you could use the % syntax for creating the string:

<%= h(%[She's the one that took me "to" ]) %>
0

精彩评论

暂无评论...
验证码 换一张
取 消