We store our blog posts in a text column in our database. We need a “preview” of the blog post for our blog’s home page. That way, we can display the last couple days’ blog posts with links to read the whole thing.
Is there a way in rails to pull out a certain amount of a text column? Maybe the first x bytes? I understand this won’t be the same length for every blog post, but it should get it in the general area.
Or maybe do some html/css hack where we overflow a div and add an elipse to the div?
I could also see creating a separate “preview” column, but that feels very hacky.
Rails 3.2.1. Database is MySQL
If I undertood correctly, you want something that transforms a long text entry into a limited preview text, like
This is a preview text, and it says that....To do that, you can simple do something like:
It basically gets the first 100 characters from the text.
If you want something more complex, that takes words in account (so you don’t end up with things like
This is a previ..., you can have a function that separates the text into words and add them to the final text until they reach the required length, like: