在Rails 3.2.11中的label_tag中呈现html(html_safe,原始格式不起作用) [英] Render html in label_tag in Rails 3.2.11 (html_safe, raw not working)

查看:66
本文介绍了在Rails 3.2.11中的label_tag中呈现html(html_safe,原始格式不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:Rails 3.2.11& Ruby 1.8.7

Using: Rails 3.2.11 & Ruby 1.8.7

我在尝试使label_tag输出html时遇到一些严重问题.基本上可以归结为:

I am having some serious problem trying to make label_tag output html. Basically it boils down to:

<%= label_tag "This will <strong>not</strong> work!" %>

我尝试过:

<%= raw label_tag "This will <strong>not</strong> work!" %>
<%= label_tag raw "This will <strong>not</strong> work!" %>
<%= label_tag "This will <strong>not</strong> work!".html_safe %>
<%= (label_tag "This will <strong>not</strong> work!").html_safe %>

我已经安装了gem'rails_xss'.

I have installed the gem 'rails_xss'.

什么都行不通!

即使我在转义html时发现了很多相关问题,但人们在使用raw和html_safe时仍然遇到问题,但与label_tag没有任何关系.我不能使用f.label来解决此问题.

Even though I can find a lot of related problems with escaping html where people having problems with raw and html_safe not working, nothing is related to label_tag. I cannot use f.label for this issue.

这曾经在同一个应用程序上运行,但是经过几次更新(其中主要版本是Rails 3.0.3-> 3.2.11),它停止了工作.我没有注意到这是什么时候发生的,所以我不确定是什么引起了问题.

This used to work on the same application but after a few updates (where Rails 3.0.3 -> 3.2.11 was the major one) it stopped working. I didn't notice when this happened so I am not sure what caused the problem.

您可以复制吗?你有解决办法吗?

Can you replicate? Do you have a solution?

推荐答案

问题是label_tag的第一个参数应该是标签名称.如果希望在标记内显示自定义内容,则必须是第二个参数.

The problem is that the first argument to label_tag is supposed to be the label name. If you wish to display custom content inside the tag, that must be the second argument.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-label_tag

尝试一下:

<%= label_tag "my label name", raw("This will <strong>not</strong> work!") %>

这篇关于在Rails 3.2.11中的label_tag中呈现html(html_safe,原始格式不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆