Ruby on Rails似乎是自动转义由link_to创建的html
这是我的代码,我试图用句子forms显示一个bboy的船员链接列表.to_sentence
0)%> 1) then "Crew".pluralize else "Crew" end %>: Independent
我得到的输出是正确的链接,但它显示为:
Hustle Kidz and Knuckleheads Cali
而不是:
Hustle Kidz和Knuckleheads Cali
与html转义,而不是所需的链接。
我错过了什么吗? 我尝试过CGI.unescapeHTML和其他一些但是迷路了……
我同意Kleber S,你应该把它变成一个帮手,因为这对于一个视图来说是很多逻辑
def crews_description(crews) if crews.empty? content_tag('em', 'Independent') else label = "Crew" label = label.pluralize if crews.size > 1 crews_links = crews.map {|crew| link_to(h(crew.name), crew)}.to_sentence content_tag('span', label) + crews_links.html_safe end end
在你看来:
<%= crews_description(@bboy.crews)
Rails 3现在自动转义所有内容,为了输出原始HTML,请使用:
<%= some_string.html_safe %>
或这个:
<%= raw @some_html_string %>
感谢macek提示。
有关其他详细信息,请访问: http : //markconnell.co.uk/posts/2010/02/rails-3-html-escaping
你可以(并且应该)使用raw
方法
<%= raw @some_html_string %>
我建议您将此代码块移动到帮助程序,然后使用.html_safe方法获取预期结果。