Jac*_*ack 11 blogs ruby-on-rails
我正在使用RoR构建一个博客.我有index.html.erb页面显示所有帖子的帖子.它显示所有帖子及其所有内容.我想将显示的内容限制为一定数量的字符,然后将"阅读更多"链接转到该个别博客帖子的显示页面.有任何帮助,如何做到这一点?谢谢.
Dam*_*ien 28
<%= truncate post.content, length: 160 %>
<%= link_to 'read more', post %>
Run Code Online (Sandbox Code Playgroud)
请参阅截断文档:http://api.rubyonrails.org/classes/String.html#method-i-truncate
mia*_*t17 14
要显示一定数量的字符,可以使用truncate helper方法截断文章.
truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."
Run Code Online (Sandbox Code Playgroud)
如果您还有关于"阅读更多"链接的问题,请从外部阅读Rails路由中的 "资源路由"部分.您应该显示所有帖子index
(可能是分页),并在show
索引中显示单个帖子.截断index
视图中的帖子,并在show
视图中显示完整的帖子.
使用truncate
帮手
truncate(text, :length => 100)
Run Code Online (Sandbox Code Playgroud)
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate