Ruby on Rails 似乎是由 link_to 创建的自动转义 html [英] Ruby on Rails seems to be auto-escaping html created by link_to

查看:33
本文介绍了Ruby on Rails 似乎是由 link_to 创建的自动转义 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我正在尝试使用 .to_sentence 以句子形式显示指向 bboy 团队的链接列表

Here is my code, I'm trying to display a list of links to a bboy's crews in sentence form with .to_sentence

<span class="affiliation">
    <% if(@bboy.crews.count > 0)%>
    <span><%= if(@bboy.crews.size > 1) then "Crew".pluralize else "Crew" end %>:</span>
    <%= @bboy.crews.collect{|c| link_to c.name, c}.to_sentence %>
    <% else %>  
    <em>Independent</em>
    <% end %>
</span>

我得到的输出是正确的链接,但显示为:

The output I get is the correct links but it displays as:

<a href="/crews/1">Hustle Kidz</a> and <a href="/crews/2">Knuckleheads Cali</a>

而不是:

Hustle Kidz指关节卡利

带有转义的 html,而不是所需的链接.

with the html escaped, rather than the desired links.

我错过了什么吗?我试过 CGI.unescapeHTML 和其他一些,但我迷路了......

Am I missing something? I've tried CGI.unescapeHTML and a few others but am getting lost...

推荐答案

我同意 Kleber S 的观点,你应该把它移到 helper 中,因为视图的逻辑很多

I agree with Kleber S, you should move this into a helper, because that's a lot of logic for a view

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

在您看来:

<span class="affiliation">
  <%= crews_description(@bboy.crews)
</span>

这篇关于Ruby on Rails 似乎是由 link_to 创建的自动转义 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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