【问题标题】:How to use HTML5 Entities in Rails 4 Helper Tags如何在 Rails 4 Helper 标签中使用 HTML5 实体
【发布时间】:2015-05-22 12:30:01
【问题描述】:

我一直在尝试使用 cdata_section、content_tag、escape_once 和 rails 上的标签助手中的标签来使“X”出现在 ✖

<%= link_to( ("&#x2716;"), url_options = {:base_rate_id => rates.id}, class: "button tiny" ) %

用不同的方法运气不好。它要么显示 nil 要么显示原始文字 "✖";有没有更好的办法?

【问题讨论】:

    标签: ruby-on-rails html tags helper entities


    【解决方案1】:

    使用ActionView::Helpers::SanitizeHelper

    <%= link_to( sanitize("&#x2716;"), url_options = {:base_rate_id => rates.id}, class: "button tiny" ) %>
    

    【讨论】:

      【解决方案2】:

      您只需按如下方式取消转义 HTML 实体:

      <%= link_to(CGI.unescapeHTML("&#x2716;"), url_options = {:base_rate_id => rates.id}, class: "button tiny" ) %>
      

      P.S.:可能你需要包含 CGI 库:

      require 'cgi'
      

      【讨论】:

        【解决方案3】:

        您可以使用以下语法

        <%= link_to("&#x2716;".html_safe, url_options = {:base_rate_id => rates.id}, class: "button tiny") %>
        

        但是你应该小心使用html_safe。确保没有用户提供的输入进入使用它呈现的字符串。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-08
          • 2013-08-20
          • 2012-03-27
          • 2014-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多