如何获得options_for_select以表彰标签

时间:2011-04-29 16:06:02

标签: html ruby-on-rails erb

我正在尝试根据其分层级别在选择的类别列表中缩进选项,例如Category Subcategory  Subsubcategory

我试过了: <% options = options_for_select @categories.map{|c| [("&nbsp;" * c.level) + c.name, c.id]} %>, 但这会呈现为&nsbp;Subcategory&nbsp;&nbsp;Subsubcategory

我试过了: <% options = options_for_select @categories.map{|c| [(" " * c.level) + c.name, c.id]} %>, 但是这会忽略空格并呈现为SubcategorySubsubcategory

作为测试,我尝试了<%= ("&nbsp;" * 3) + "hello" %>,这使我的期望如下:   hello

有谁知道如何获得我正在寻找的结果?

1 个答案:

答案 0 :(得分:1)

尝试使用(("&nbsp;" * c.level) + c.name).html_safe。问题在于使用options_for_select的{​​{1}}辅助方法,它会使html_escape实体失效。添加&nbsp;将允许从html_safe帮助程序调用的html_escape方法,以避免转义空格。