0

I was wondering if it was possible to use jquery on the truncate helper method in rails. I have made the omission symbol a link_to like so

<%= raw(truncate(@book.description, :length => 1000, :omission => (link_to' (continued)', :id => 'revealMore'))) %>

I have given it an ID as I assume thats what I will need to select it in jquery, I'm just a little unsure on how to go about it and whether all the text is stored when truncating using the helper method

Any one done this before

Thanks

Richlewis
  • 15,070
  • 37
  • 122
  • 283

1 Answers1

1

I don't think you can use truncate directly, but you can write another helper that calls it. This isn't exactly what you're looking for (it doesn't go back to the closed state), but similar to what you're asking for. You may be able to tweak and improve upon it.

  def more(string, length, separator = nil)
    return '' if !string
    if string.length > (length * 1.2)
      out = content_tag(:span, truncate(string, length: length, separator: separator))
      out += link_to_function('more', "$(this).prev().hide(); $(this).hide(); $(this).next().show();")
      out += content_tag(:span, string, :style => 'display:none')
      out
    else
      string
    end
  end
Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70