raw vs. html_safe vs. h to unescape html

前端 未结 6 1545

Suppose I have the following string

@x = \"Turn me into a link\"

In my view, I want a link to be displayed.

6条回答
  •  佛祖请我去吃肉
    2020-11-22 05:58

    I think it bears repeating: html_safe does not HTML-escape your string. In fact, it will prevent your string from being escaped.

    <%= "" %>
    

    will put:

    <script>alert('Hello!')</script>
    

    into your HTML source (yay, so safe!), while:

    <%= "".html_safe %>
    

    will pop up the alert dialog (are you sure that's what you want?). So you probably don't want to call html_safe on any user-entered strings.

提交回复
热议问题