Rails truncate helper with link as omit text

前端 未结 7 1880
忘掉有多难
忘掉有多难 2021-02-06 01:27

I\'m quite long description that I want to truncate using truncate helper. So i\'m using the:

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


        
7条回答
  •  执笔经年
    2021-02-06 01:59

    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.

提交回复
热议问题