简体   繁体   中英

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

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;  &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" ]) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM