我想在我的Rails 5应用程序中明确地知道何时运行(并且当不是因为它自动运行时)sanitize
方法。
例如,在我的视图文件中,嵌入在<%= ... %>
中,我应该使用这些吗?
sanitize(record.value)
link_to(sanitize(record.value), ...)
tag.span(sanitize(record.value))
t("translation_string_html", :argument_value => sanitize(record.value))
还是这些?
record.value
link_to(record.value, ...)
tag.span(record.value)
t("translation_string_html", :argument_value => record.value)
一般使用Rails助手时不需要消毒。
例如,如果您按照link_to
方法结束,则此处是相关行
def content_tag_string(name, content, options, escape = true)
tag_options = tag_options(options, escape) if options
content = ERB::Util.unwrapped_html_escape(content) if escape
"<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe
end
正如你所看到的那样,两个html都是用Erb中的一些工具来逃避,然后在最终返回之前还使用html_safe
。