LoginSignup
1
0

More than 3 years have passed since last update.

続きを読む(truncate)の実装

Last updated at Posted at 2021-01-22

環境

・rails 5.2.4

続きを読むの実装

snsでよくある続きを読むの実装の紹介をさせて頂きます。。

とっても簡単で以下のように末尾にtruncateと書くだけです。

images/index.html.erb
<%= image.introduction.truncate(20) %>

20文字以上で...で表示されるという意味です。

この記述をすれば、以下のような表示になります。
image.png

ちなみに、続きを読むのリンクは以下の記述で実装できました。

images/index.html.erb

<%= image.body.truncate(20) %>
  <% if image.body.size >= 20 %>
    <%= link_to image_path(image.id) do %>
       <p>続きを読む</p>
    <% end %>
  <% end %>

20文字以上だと続きを読むが表示されるというif文です。

簡単ですが、説明は以上となります。
ありがとうございました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0