[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 chart kick
[For those who create portfolios] How to use binding.pry with Docker
[For those who create portfolios] Search function created with ransack
How to create a Maven repository for 2020
[For those who create portfolios] Reduce mistakes and make the code easier to read -Rubocop Airbnb-
How to create a database for H2 Database anywhere
[java] Summary of how to handle character strings
How to create docker-compose
How to concatenate strings
[Rails] How to create a signed URL for CloudFront
[Spring Boot] How to create a project (for beginners)
How to create an application
Create an Android app for those who don't want to play music on the speaker
How to create a lightweight container image for Java apps
VS Code FAQ for those who want to escape Eclipse
How to create a method
How to create and launch a Dockerfile for Payara Micro
How to change arguments in [Java] method (for those who are confused by passing by value, passing by reference, passing by reference)
[Rails] How to omit the display of the character string of the link_to method
How to specify validation for time_field
How to install JMeter for Mac
How to enclose any character with "~"
[Java] How to create a folder
[Swift] How to replace multiple strings
How to concatenate strings in java
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to use binding.pry for view files
How to install Play Framework 2.6 for Mac
Sample to create custom tag for JSP
[Swift5] How to create a splash screen
[rails] How to create a partial template
Needed for iOS 14? How to set NSUserTrackingUsageDescription
[For beginners] How to debug in Eclipse
How to make a judgment method to search for an arbitrary character in an array
For those who get the error Unprintable ASCII character found in source file