raw vs. html_safe vs. h to unescape html

前端 未结 6 1522

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:49

    The difference is between Rails’ html_safe() and raw(). There is an excellent post by Yehuda Katz on this, and it really boils down to this:

    def raw(stringish)
    
      stringish.to_s.html_safe
    
    end
    

    Yes, raw() is a wrapper around html_safe() that forces the input to String and then calls html_safe() on it. It’s also the case that raw() is a helper in a module whereas html_safe() is a method on the String class which makes a new ActiveSupport::SafeBuffer instance — that has a @dirty flag in it.

    Refer to "Rails’ html_safe vs. raw".

提交回复
热议问题