Reputation: 231
In my app, I have a comments section with a body.
<p><%= truncate(comment.body, length: 550) %> <%= link_to "Read More" %></p>
I currently have this code to only show a portion of the comment's body, but when in the Link_to, when the user clicks on Read More, it disables the truncate method and show the full body
How would I go by doing that?
Thanks
Upvotes: 2
Views: 3478
Reputation: 141
You can use readmore-rails gem for nice toggle of long text.
<script>
$(document).ready(function() {
$('article').readmore({
collapsedHeight: 218,
});
$('article').readmore({
collapsedHeight: 218,
});
$('article').removeClass('hidden');
});
</script>
<article>
<%= @post.text.html_safe %>
</article>
Upvotes: 2
Reputation: 101
You won't be able to achieve this using truncate, you'll need the entire string and then hide part of it using Javascript. When a user clicks the Read More link, you would use Javascript to show the hidden part.
There's a good explanation in a previous question jQuery text truncation (read more style)
Upvotes: 1