Rails - 自定义html到simple_form标签

时间:2021-04-21 20:16:09

I'm trying to customize the output of a simple_form association, basically I need to display a checkbox label on two lines. My idea was adding a "br" tag into the "label", but unfortunately it gets escaped so it display actually "br" instead of going to a new line

我正在尝试自定义simple_form关联的输出,基本上我需要在两行上显示复选框标签。我的想法是在“标签”中添加一个“br”标签,但不幸的是它被转义,所以它显示实际上是“br”而不是去一个新线

I use a lambda for customizing the label output

我使用lambda来自定义标签输出

<%= f.association :item, :as => :check_boxes, :collection => current_user.items, :label => false, :label_method => lambda { |item| "#{item.city.capitalize},<br> #{item.address}" }%>

this produces an escaped br into the label string, how could I display the label on two lines?

这会在标签字符串中生成一个转义的br,我怎样才能在两行上显示标签?

3 个解决方案

#1


28  

call html_safe method on the string you want not to be escaped.

在你想要不被转义的字符串上调用html_safe方法。

<%= f.association :item, :as => :check_boxes, :collection => current_user.items, :label => false, :label_method => lambda { |item| "#{item.city.capitalize},<br> #{item.address}".html_safe }%>

#2


12  

For those of you looking to have custom html in elements as the title of the OP's question suggests, you can do this:

对于那些希望在OP问题的标题中有自定义html元素的人,你可以这样做:

= f.input(:Foo, label: "Foo <span>(Blah helper text blah)</span>".html_safe)

#3


2  

Does html_safe help?

html_safe有帮助吗?

<%= f.association(....).html_safe %>

if not, then post an example app on github showcasing this issue, so we can debug it

如果没有,那么在github上发布一个示例应用程序来展示这个问题,所以我们可以调试它

#1


28  

call html_safe method on the string you want not to be escaped.

在你想要不被转义的字符串上调用html_safe方法。

<%= f.association :item, :as => :check_boxes, :collection => current_user.items, :label => false, :label_method => lambda { |item| "#{item.city.capitalize},<br> #{item.address}".html_safe }%>

#2


12  

For those of you looking to have custom html in elements as the title of the OP's question suggests, you can do this:

对于那些希望在OP问题的标题中有自定义html元素的人,你可以这样做:

= f.input(:Foo, label: "Foo <span>(Blah helper text blah)</span>".html_safe)

#3


2  

Does html_safe help?

html_safe有帮助吗?

<%= f.association(....).html_safe %>

if not, then post an example app on github showcasing this issue, so we can debug it

如果没有,那么在github上发布一个示例应用程序来展示这个问题,所以我们可以调试它