Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show only some words from post as preview

I am using Ruby on Rails 3.2.1 and model @post. Problem is that I want to show only first paragraph or some words on my first app page like a preview, not all the content. How I can do that?

index:

<% @posts.each{|posts| %>
        <h1 class="title"><%= link_to posts.title, posts %></h1>
            <p class="byline" style="text-align: right; font-weight: bold;">Raksts izveidots: <%= posts.created_at.utc.strftime("%d.%m.%Y") %></p>
            <div class="entry">
                <p><%= raw posts.content %></p>
            </div>
        <p class="meta"><a href="#" class="more">Read More</a> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <a href="#" class="comments">Comments</a> (33)</p>           
    <% } %>
like image 542
RydelHouse Avatar asked Nov 28 '22 17:11

RydelHouse


2 Answers

Maybe I'm not understanding the question but if content is a string

posts.content[0,20]

will give you the first 20 characters.

like image 20
EricM Avatar answered Dec 07 '22 01:12

EricM


Know the builtin tools.

like image 195
apneadiving Avatar answered Dec 06 '22 23:12

apneadiving