gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:34:58 24 4
gpt4 key购买 nike

这是我想要的字符串:

<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_saferaw ,但它们似乎没有任何影响。

最佳答案

从 Rails 5 开始,有一个 to_sentence执行此操作的 View 助手(不同于 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))}) %>

关于ruby-on-rails - to_sentence 和 html_safe,一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9696239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com