I'm building a blog using RoR. I have the index.html.erb page for the posts showing all of the posts. It displays all of the posts and all of their content. I'd like to limit the content that is shown to a certain number of characters and then put a "read more" link to go to the show page for that individual blog post. Any help with how to do this? Thanks.
我正在使用RoR构建一个博客。我有index.html.erb页面显示所有帖子的帖子。它显示所有帖子及其所有内容。我想将显示的内容限制为一定数量的字符,然后将“阅读更多”链接转到该个别博客帖子的显示页面。有任何帮助,如何做到这一点?谢谢。
3 个解决方案
#1
12
To show a certain number of characters, you can use truncate helper method to truncate your article.
要显示一定数量的字符,可以使用truncate helper方法截断文章。
truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."
If you also have question about "read more" link, please read "resource routing" section in Rails Routing from the Outside In. You should show all your posts in index
action (probably with pagination), and show single post in show
index. Truncate the post in the index
view, and show the full post in show
view.
如果您还有关于“阅读更多”链接的问题,请从外部输入阅读Rails路由中的“资源路由”部分。您应该在索引操作中显示所有帖子(可能带有分页),并在show index中显示单个帖子。在索引视图中截断帖子,并在show view中显示完整的帖子。
#2
27
<%= truncate post.content, length: 160 %>
<%= link_to 'read more', post %>
See the documentation for truncate: http://api.rubyonrails.org/classes/String.html#method-i-truncate
请参阅截断文档:http://api.rubyonrails.org/classes/String.html#method-i-truncate
#3
8
Use truncate
helper
使用truncate helper
truncate(text, :length => 100)
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate
#1
12
To show a certain number of characters, you can use truncate helper method to truncate your article.
要显示一定数量的字符,可以使用truncate helper方法截断文章。
truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."
If you also have question about "read more" link, please read "resource routing" section in Rails Routing from the Outside In. You should show all your posts in index
action (probably with pagination), and show single post in show
index. Truncate the post in the index
view, and show the full post in show
view.
如果您还有关于“阅读更多”链接的问题,请从外部输入阅读Rails路由中的“资源路由”部分。您应该在索引操作中显示所有帖子(可能带有分页),并在show index中显示单个帖子。在索引视图中截断帖子,并在show view中显示完整的帖子。
#2
27
<%= truncate post.content, length: 160 %>
<%= link_to 'read more', post %>
See the documentation for truncate: http://api.rubyonrails.org/classes/String.html#method-i-truncate
请参阅截断文档:http://api.rubyonrails.org/classes/String.html#method-i-truncate
#3
8
Use truncate
helper
使用truncate helper
truncate(text, :length => 100)
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate