Rails 截断助手,其中链接为省略文本

发布于 2024-10-18 01:48:40 字数 433 浏览 5 评论 0原文

我的描述很长,我想使用截断助手来截断。 所以我正在使用:

truncate article.description, :length => 200, :omission => ' ...'

问题是我想使用 more 作为可点击的链接,所以理论上我可以使用这个:

truncate article.description, :length => 200, :omission => "... #{link_to('[more]', articles_path(article)}"

省略文本被视为不安全,因此被转义。我试图将其设置为 html_safe 但它不起作用,而不是链接 [更多] 我的浏览器仍然显示该链接的 html。

有没有办法强制截断打印省略链接而不是省略文本?

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)}"

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

关于从前 2024-10-25 01:48:40

使用 Rails 4,您可以/应该传递链接块:

truncate("Once upon a time in a world far far away", 
  length: 10, 
  separator: ' ', 
  omission: '... ') {     
    link_to "Read more", "#" 
}

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", "#" 
}
在你怀里撒娇 2024-10-25 01:48:40

我建议您在辅助方法中自行执行此操作,这样您也可以对输出有更多的控制:

def article_description article
  output = h truncate(article.description, length: 200, omission: '...')
  output += link_to('[more]', article_path(article)) if article.description.size > 200
  output.html_safe
end

I would suggest doing this on your own in a helper method, that way you'll have a little more control over the output as well:

def article_description article
  output = h truncate(article.description, length: 200, omission: '...')
  output += link_to('[more]', article_path(article)) if article.description.size > 200
  output.html_safe
end
流年里的时光 2024-10-25 01:48:40

肮脏的解决方案...使用“原始”方法来取消转义它。
您必须确保内容的“合理性”。

raw(truncate article.description, :length => 200, :omission => "... #{link_to('[more]', articles_path(article)}")

raw 是一个类似于 html_safe 的助手。
bye

edit: 不是转义的遗漏,而是truncate方法的结果。

Dirty solution... use the method "raw" to unescape it.
you have to be sure of "sanity" of your content.

raw(truncate article.description, :length => 200, :omission => "... #{link_to('[more]', articles_path(article)}")

raw is a helper acting like html_safe .
bye

edit: is not the omission of being escaped , but the result of truncate method.

源来凯始玺欢你 2024-10-25 01:48:40

我遇到了类似的情况,这解决了问题。尝试(为了可读性而换行):

(truncate h(article.description), 
                  :length => 200, 
                  :omission => "... #{link_to('[more]',articles_path(article)}")
                  .html_safe

您可以使用 h 来确保文章描述的完整性,并且由于您将 link_to 设置为您知道不是潜在恶意的路径,因此您可以将结果字符串标记为 html_safe ,无需担心。

I encountered a similar situation and this did the trick. Try (line breaks for readability):

(truncate h(article.description), 
                  :length => 200, 
                  :omission => "... #{link_to('[more]',articles_path(article)}")
                  .html_safe

You can use h to ensure sanity of article description, and since you are setting the link_to to a path you know to not be something potentially nefarious, you can mark the resulting string as html_safe without concern.

橘和柠 2024-10-25 01:48:40

TextHelper#truncate 有一个 truncate 的形式,它允许您使用未转义的 link_to,同时仍转义截断的文本:

truncate("<script>alert('hello world')</script>") { link_to "Read More", "#" }

#=> <script>alert('hello world'...<a href="#">Read More</a>

TextHelper#truncate has a block form of truncate, which lets you use a link_to that isn't escaped, while still escaping the truncated text:

truncate("<script>alert('hello world')</script>") { link_to "Read More", "#" }

#=> <script>alert('hello world'...<a href="#">Read More</a>
眼趣 2024-10-25 01:48:40

唯一对我有用的:

<%= truncate(@article.content, length: 200, omission: " ... %s") % link_to('read more', article_path(@article)) %>

The only one that worked for me :

<%= truncate(@article.content, length: 200, omission: " ... %s") % link_to('read more', article_path(@article)) %>
用心笑 2024-10-25 01:48:40

我遇到了同样的问题,就我而言,我只是使用 :escape =>;假
有效:

truncatearticle.description, :length => 200、:省略=> "... #{link_to('[more]',articles_path(article)}", :escape => false

来自文档:

结果被标记为 HTML 安全,但默认情况下会进行转义,除非 :escape 为 false...
链接: http://apidock.com/rails/ActionView/Helpers/TextHelper/truncate< /a>


I had the same problem, in my case i just used :escape => false.
That worked:

truncate article.description, :length => 200, :omission => "... #{link_to('[more]', articles_path(article)}", :escape => false

From documentation :

The result is marked as HTML-safe, but it is escaped by default, unless :escape is false....
link: http://apidock.com/rails/ActionView/Helpers/TextHelper/truncate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文