Content_tag会转义数据

时间:2014-04-14 08:02:42

标签: ruby-on-rails ruby ruby-on-rails-4

span_tag = content_tag(:span, class: "123")

它生成以下html:

<span>{:class=&gt;&quot;123&quot;}</span>

我想知道,这是什么?难道不仅仅是<span class="123"></span>这正是我想要的吗?我是否必须使用.html_safe

2 个答案:

答案 0 :(得分:5)

尝试:

span_tag = content_tag(:span, nil, class: "123")

答案 1 :(得分:0)

来自doc

  

content_tag(name,content_or_options_with_block = nil,options = nil,   escape = true,&amp; block)

传递空块:

span_tag = content_tag(:span, class: "123") {}
"<span class=\"123\"></span>"
span_tag = content_tag(:span, class: "123") { 'foo' }
"<span class=\"123\">foo</span>"