[For those who create portfolios] How to omit character strings

When creating a portfolio, I wanted to omit the long sentences and display "Read more" or "...", so I will summarize them here.

Conclusion

Use the truncate method.

How to use

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

Result of actual use

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 %>
スクリーンショット 2020-07-20 19.03.45.png 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.

reference

https://apidock.com/rails/String/truncate https://apidock.com/rails/ActionView/Helpers/TextHelper/truncate

Recommended Posts

[For those who create portfolios] How to omit character strings
[For those who create portfolios] How to use binding.pry with Docker
How to concatenate strings
How to create a method
How to create and launch a Dockerfile for Payara Micro
Display character strings character by character [Note]
[Rails] How to omit the display of the character string of the link_to method
[Swift] How to replace multiple strings
How to concatenate strings in java
Easy way to create JSP custom tags
How to execute WebCamCapture sample of NyARToolkit for Java
I want to create a generic annotation for a type
[For those who create portfolios] How to omit character strings
Tutorial to create a blog with Rails for beginners Part 2
How to create and launch a Dockerfile for Payara Micro
Tutorial to create a blog with Rails for beginners Part 0
Needed for iOS 14? How to set NSUserTrackingUsageDescription
[For beginners] How to debug in Eclipse