加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – to_sentence和html_safe,在一起?

发布时间:2020-12-17 03:02:26 所属栏目:百科 来源:网络整理
导读:这是我想要的字符串: a href="/pugs/1-baxter"Baxter/a and a href="/pugs/2-sofia"Sofia/a 这是我用来输出的代码: %= @pugs.collect {|p| link_to(p.name,pug_path(p))}.to_sentence % 不幸的是输出被编码: lt;a href=quot;/pugs/1-baxterquot;gt;Baxter
这是我想要的字符串:

<a href="/pugs/1-baxter">Baxter</a> and <a href="/pugs/2-sofia">Sofia</a>

这是我用来输出的代码:

<%= @pugs.collect {|p| link_to(p.name,pug_path(p))}.to_sentence %>

不幸的是输出被编码:

&lt;a href=&quot;/pugs/1-baxter&quot;&gt;Baxter&lt;/a&gt; and &lt;a href=&quot;/pugs/2-sofia&quot;&gt;Sofia&lt;/a&gt;

我尝试过使用html_safe和raw,但它们似乎没有任何影响.

解决方法

从Rails 5开始,有一个to_sentence视图助手(与Array#to_sentence不同)就是这样做的.

从the docs开始:

to_sentence(array,options = {})
Converts the array to a comma-separated sentence where the last element is joined by the
connector word. This is the html_safe-aware version of ActiveSupport’s
Array#to_sentence.

使用它像:<%to_sentence(@pugs.collect {| p | link_to(p.name,pug_path(p))})%>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读