therod
therod

Reputation: 174

Ruby on Rails - Truncate to a specific string

Clarification: The creator of the post should be able to decide when the truncation should happen.

I implemented a Wordpress like [---MORE---] functionality in my blog with following helper function:

# application_helper.rb

def more_split(content)
split = content.split("[---MORE---]")
split.first
end

def remove_more_tag(content)
content.sub(“[---MORE---]", '')
end

In the index view the post body will display everything up to (but without) the [---MORE---] tag.

# index.html.erb
<%= raw more_split(post.rendered_body) %>

And in the show view everything from the post body will be displayed except the [---MORE---] tag.

# show.html.erb
<%=raw remove_more_tag(@post.rendered_body) %>

This solution currently works for me without any problems. Since I am still a beginner in programming I am constantly wondering if there is a more elegant way to accomplish this.

How would you do this?

Thanks for your time.


This is the updated version:

# index.html.erb
<%=raw truncate(post.rendered_body,  
                :length => 0, 
                :separator => '[---MORE---]', 
                :omission => link_to( "Continued...",post)) %>

...and in the show view:

# show.html.erb
<%=raw (@post.rendered_body).gsub("[---MORE---]", '') %>

Upvotes: 4

Views: 6810

Answers (3)

Sahil Grover
Sahil Grover

Reputation: 1915

I tried and found this one is the best and easiest

def summarize(body, length)
    return simple_format = body[0..length]+'...'
end

s = summarize("to get the first n characters in your post. So, then you don't have to deal w/ splitting on the [---MORE---]  post.body.",20)


ruby-1.9.2-p290 :017 > s
=> "to get the first n ..." 

Upvotes: 0

dombesz
dombesz

Reputation: 7909

I would use just simply truncate, it has all of the options you need.

truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
# => "And they f... (continued)"

Update

After sawing the comments, and digging a bit the documentation it seems that the :separator does the work.

From the doc:

Pass a :separator to truncate text at a natural break.

For referenece see the docs

truncate(post.rendered_body, :separator => '[---MORE---]')

On the show page you have to use gsub

Upvotes: 8

kafuchau
kafuchau

Reputation: 5593

You could use a helper function on the index page that only grabs the first X characters in your string. So, it would look more like:

<%= raw summarize(post.rendered_body, 250) %>

to get the first 250 characters in your post. So, then you don't have to deal w/ splitting on the [---MORE---] string. And, on the show page for your post, you won't need to do anything at all... just render the post.body.


Here's an example summarize helper (that you would put in application_helper.rb):

def summarize(body, length)
return simple_format(truncate(body.gsub(/<\/?.*?>/,  ""), :length => length)).gsub(/<\/?.*?>/,  "")
end

Upvotes: 0

Related Questions