views:

175

answers:

1

I have

<%=link_to topic.title, topic_path(topic) %>
- <%= truncate(topic.description_without_embed, :length=>50, :omission =>"...") %>

But what I really want to do is to truncate both the title and the post to 50 characters. How can this be done?

The output should look something like

Lorem ipsum dolor sit amet, consectetur adipiscing elit. - Curabitur eu dolor est, id pharetra lectus. Phasellus nec feugiat dolor. Curabitur feugiat ultrices leo ut egestas. Nullam nec nisi in ligula feugiat placerat. Ut adipiscing urna consequat tortor euismod pretium. Maecenas eget quam aliquet orci blandit eleifend eu et quam. Donec.

instead of

Lorem ipsum dolor sit amet, consectetur adipiscing elit. - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur eu dolor est, id pharetra lectus. Phasellus nec feugiat dolor. Curabitur feugiat ultrices leo ut egestas. Nullam nec nisi in ligula feugiat placerat. Ut adipiscing urna consequat tortor euismod pretium. Maecenas eget quam aliquet orci blandit eleifend eu et quam. Donec.

I don't need to use the truncate method, any method would be helpful. Thanks.

+1  A: 

You can just concatenate the two parts and truncate the result. Maybe factor it out into a helper method as well:

def truncate_topic(topic)
  full_text = link_to(topic.title, topic) + ' - ' + topic.description_without_embed
  truncate(full_text, :length => 50, :omission => "...")
end

And then in your view:

<%= truncate_topic(topic) %>
Jimmy Cuadra
thank you so much :)
Quhby
If my answer solves the problem for you, you can accept it by clicking the check mark outline on the left. :)
Jimmy Cuadra