When creating a portfolio, I wanted to omit the long sentences and display "Read more" or "...", so I will summarize them here.
Use the truncate method.
There are two types of truncate.
String
string.rb
"Spring is Akebono. At the edge of the mountain, which is finally turning white, a little light is shining, and the thin clouds of purple clouds flutter.".truncate(30) %>
# "Spring is Akebono. At the edge of the mountain, which is finally turning white, a little light,..."
option | Description | Default |
---|---|---|
:omission | The string that follows the abbreviated string | ... |
:separator | Delimiter. Make sure that the character string is not cut off halfway. I can't do it in Japanese. | None |
With these,
string.rb
"Spring is Akebono. At the edge of the mountain, which is finally turning white, a little light is shining, and the thin clouds of purple clouds flutter.".truncate(30, omission: '...read more', separator: ' ')
# "Spring is Akebono. Finally whitening on the mountainside, few...read more"
TextHelper
texthelper.rb
truncate("Spring is Akebono. At the edge of the mountain, which is finally turning white, a little light is shining, and the thin clouds of purple clouds flutter.")
# "Spring is Akebono. At the edge of the mountain, which is finally turning white, a little light,..."
option | Description | Default |
---|---|---|
:length | Character string length before abbreviation | 30 |
:omission | The string that follows the abbreviated string | ... |
:separator | Delimiter. Make sure that the character string is not cut off halfway. I can't do it in Japanese. | None |
:escape | HTML escape(Do not escape if false is specified) | true |
I used it to display the content of the person who posted it.
<%= truncate(@post.content, length: 50) do %>
<p><%= link_to 'read more', @post %></p>
<% end %>
When you enter the weekly report, the post will be displayed on the right side, and after displaying 50 characters of the posted content, you can jump to the details with the link to read more.
https://apidock.com/rails/String/truncate https://apidock.com/rails/ActionView/Helpers/TextHelper/truncate
Recommended Posts