Rails – 自定义html到simple_form标签

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

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

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

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

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

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

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

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

html_safe帮助吗?

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

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