Ruby on Rails – 截断到特定的字符串
发布时间:2020-12-17 02:51:54 所属栏目:百科 来源:网络整理
导读:澄清:帖子的创建者应该能够决定截断何时发生. 我在我的博客中使用以下辅助函数实现了类似[— MORE —]功能的Wordpress: # application_helper.rbdef more_split(content)split = content.split("[---MORE---]")split.firstenddef remove_more_tag(content)
澄清:帖子的创建者应该能够决定截断何时发生.
我在我的博客中使用以下辅助函数实现了类似[— MORE —]功能的Wordpress: # application_helper.rb def more_split(content) split = content.split("[---MORE---]") split.first end def remove_more_tag(content) content.sub(“[---MORE---]",'') end 在索引视图中,帖子正文将显示(但没有)[— MORE —]标记的所有内容. # index.html.erb <%= raw more_split(post.rendered_body) %> 在节目视图中,除了[— MORE —]标签外,还会显示帖子正文中的所有内容. # show.html.erb <%=raw remove_more_tag(@post.rendered_body) %> 这个解决方案目前对我没有任何问题. 你会怎么做? 谢谢你的时间. 这是更新版本: # index.html.erb <%=raw truncate(post.rendered_body,:length => 0,:separator => '[---MORE---]',:omission => link_to( "Continued...",post)) %> ……并在展示视图中: # show.html.erb <%=raw (@post.rendered_body).gsub("[---MORE---]",'') %> 解决方法
我只使用截断,它具有您需要的所有选项.
truncate("And they found that many people were sleeping better.",:length => 25,:omission => '... (continued)') # => "And they f... (continued)" 更新 在看完评论并挖掘了一些文档后,似乎:separator完成了工作. 来自doc: Pass a :separator to truncate text at a natural break. 对于referenece,请参阅docs truncate(post.rendered_body,:separator => '[---MORE---]') 在显示页面上,您必须使用gsub (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |