raw vs. html_safe vs. h to unescape html

前端 未结 6 1502

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 06:05

    1. html_safe :

      Marks a string as trusted safe. It will be inserted into HTML with no additional escaping performed.

      "Hello".html_safe
      #=> "Hello"
      
      nil.html_safe
      #=> NoMethodError: undefined method `html_safe' for nil:NilClass
      
    2. raw :

      raw is just a wrapper around html_safe. Use raw if there are chances that the string will be nil.

      raw("Hello")
      #=> "Hello"
      
      raw(nil)
      #=> ""
      
    3. h alias for html_escape :

      A utility method for escaping HTML tag characters. Use this method to escape any unsafe content.

      In Rails 3 and above it is used by default so you don't need to use this method explicitly

提交回复
热议问题