2

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

PMP
  • 231
  • 6
  • 25

2 Answers2

2

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>
Alex T.
  • 141
  • 1
  • 13
1

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)

Community
  • 1
  • 1
robb
  • 101
  • 1
  • 1
  • 5
  • http://stackoverflow.com/questions/15262503/rails-truncate-with-a-read-more-toggle This seems to work perfectly, don't know why I hadn't found it before asking this question, Thanks Anyways! – PMP Jun 08 '13 at 16:41