【问题标题】:How to get only numbers of a string in a link helper如何在链接助手中仅获取字符串的数字
【发布时间】:2017-04-05 00:40:09
【问题描述】:

大家好,我需要你的帮助。

在我的索引视图中,我有很多带有 disqus cmets 的帖子,我为每个帖子显示了一个评论计数器,一切正常,但我得到了例如:

0 cmets、1 条评论、2 cmets ... 等等。

我怎样才能只显示数字?例如:

0、1、2

我的助手在这里:

= link_to "", post_path(post, anchor: "disqus_thread"), data: { "disqus-identifier" => "#{post.id}" }, class: "no-underline bold blue label"

希望您的回答非常感谢!

【问题讨论】:

    标签: ruby-on-rails ruby helper


    【解决方案1】:

    你可以这样做

    = link_to "", post_path(post, anchor: "disqus_thread"), data: { "disqus-identifier" => "#{post.id.gsub('comments','')}" }, class: "no-underline bold blue label"
    

    【讨论】:

    • 你好 Kumar 我试过但没用 这是我的助手 = link_to "", post_path(post, anchor: "disqus_thread"), data: { "disqus-identifier" => "#{url_for([ post, {only_path: false}]).gsub('cmets','')}" }, class: "无下划线粗体蓝色标签" 谢谢
    • @JoelGarcia - 你可以试试这个:gsub(/comment[s]*/,'')
    • 我可以用这个。评论计数链接文本可以在您的 Disqus 管理员 > 设置 > 社区页面进行自定义。 “评论计数链接”部分。从字段disqus.com/admin/settings/community 中删除评论和评论但是现在,我怎样才能在索引视图中实时更新计数器,就像显示视图一样?因为索引视图中的计数器会在我发表评论的几分钟后更新,而显示视图中的计数器会实时更新:(我该如何解决?谢谢
    • a = '0 comments, 1 comment , 2 comments' => a.scan(/\d+/) #=> ["0", "1", "2"] - 没有?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多