Rails截断带有链接的帮助程序,省略文本 [英] Rails truncate helper with link as omit text
问题描述
我的描述很长,我想使用截断助手来截断. 所以我正在使用:
I'm quite long description that I want to truncate using truncate helper. So i'm using the:
truncate article.description, :length => 200, :omission => ' ...'
问题是我想使用更多作为可点击链接,因此从理论上讲,我可以使用它:
The problem is that I want to use more as a clickable link so in theory I could use this:
truncate article.description, :length => 200, :omission => "... #{link_to('[more]', articles_path(article)}"
遗漏文本被视为不安全,因此已被转义.我试图将其设置为html_safe,但是它没有起作用,而不是链接[more]我的浏览器仍在显示该链接的html.
Omission text is handled as unsafe so it's escaped. I tried to make it html_safe but it didn't work, instead of link [more] my browser is still showing the html for that link.
有什么方法可以强制截断来打印省略链接而不是省略文本?
Is there any way to force truncate to print omission link instead of omission text?
推荐答案
使用Rails 4,您可以/应该为该链接传递一个块:
With Rails 4, you can/should pass in a block for the link:
truncate("Once upon a time in a world far far away",
length: 10,
separator: ' ',
omission: '... ') {
link_to "Read more", "#"
}
这篇关于Rails截断带有链接的帮助程序,省略文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!