在 content_tag 块中呈现文本和 html
rendering text and html in a block of content_tag
我正在尝试使用帮助程序文件中的 rails 帮助程序生成以下 html:
<span class="btn btn-default btn-file">
Browse File<%= file_field_tag :file %>
</span>
<input type="text" class="form-control file-display" placeholder="" style="width: 250px; display: inline">
这是我试过的:
def file_helper(form, attr)
span = content_tag :span, class:"btn btn-default btn-file" do
"Browse File" + form.file_field(attr)
end
input = content_tag(:input, nil, type: 'text', class:"form-control file-display", style:"width: 250px; display: inline")
span + input
end
<%= file_helper f, :document %>
不幸的是,结果 html 看起来像这样:
<span class="btn btn-default btn-file">Browse File<input id="document_document" name="document[document]" type="file" /></span>
<input class="form-control file-display" style="width: 250px; display: inline" type="text">
问题是跨度的嵌套输入未呈现为 html。它与 "Browse File" 一起呈现为文本。如何让 "Browse File" 在外部范围内呈现为文本并使输入呈现为 html?
尝试(span + input).html_safe
Check this out...
在组合 "safe" 由 SafeBuffer 包装的字符串和不是的不安全字符串时,您 运行 遇到了问题。
SafeBuffer inherits from String, overriding +, concat and << so that:
- If the other String is safe (another SafeBuffer), the buffer concatenates it directly
- If the other String is unsafe (a plain String), the buffer escapes it first, then concatenates it
(source: click)
在您的情况下,form.file_field(attr)
会产生适当的安全缓冲区,但 "Browse File" 不会。因此,当它们组合在一起时,它们现在是一个普通的字符串,没有被 SafeBuffer 包裹。
在最后一行中,您再次将正确包装的字符串 input
与先前未包装的字符串 span
组合,这导致 span
被转义。
.html_safe
为字符串创建一个 SafeBuffer 包装器,因此包装 "Browse File".html_safe + form.file_field(attr)
应该可以解决问题。
我正在尝试使用帮助程序文件中的 rails 帮助程序生成以下 html:
<span class="btn btn-default btn-file">
Browse File<%= file_field_tag :file %>
</span>
<input type="text" class="form-control file-display" placeholder="" style="width: 250px; display: inline">
这是我试过的:
def file_helper(form, attr)
span = content_tag :span, class:"btn btn-default btn-file" do
"Browse File" + form.file_field(attr)
end
input = content_tag(:input, nil, type: 'text', class:"form-control file-display", style:"width: 250px; display: inline")
span + input
end
<%= file_helper f, :document %>
不幸的是,结果 html 看起来像这样:
<span class="btn btn-default btn-file">Browse File<input id="document_document" name="document[document]" type="file" /></span>
<input class="form-control file-display" style="width: 250px; display: inline" type="text">
问题是跨度的嵌套输入未呈现为 html。它与 "Browse File" 一起呈现为文本。如何让 "Browse File" 在外部范围内呈现为文本并使输入呈现为 html?
尝试(span + input).html_safe
Check this out...
在组合 "safe" 由 SafeBuffer 包装的字符串和不是的不安全字符串时,您 运行 遇到了问题。
SafeBuffer inherits from String, overriding +, concat and << so that:
- If the other String is safe (another SafeBuffer), the buffer concatenates it directly
- If the other String is unsafe (a plain String), the buffer escapes it first, then concatenates it
(source: click)
在您的情况下,form.file_field(attr)
会产生适当的安全缓冲区,但 "Browse File" 不会。因此,当它们组合在一起时,它们现在是一个普通的字符串,没有被 SafeBuffer 包裹。
在最后一行中,您再次将正确包装的字符串 input
与先前未包装的字符串 span
组合,这导致 span
被转义。
.html_safe
为字符串创建一个 SafeBuffer 包装器,因此包装 "Browse File".html_safe + form.file_field(attr)
应该可以解决问题。