Stiven Frams
Stiven Frams

Reputation: 127

rails Display only the first 50 characters field in view

Display only the first 50 characters <%= news.header %>
view:

<% @news.each do |news| %>
          <div class="news">
      <div class="title"> <%= news.title %> </div>
    <div class="photo"><%= image_tag( "1.jpg", :width =>247, :crop => :fit) %> </div>
      <div class="header"> <%= news.header %></div>
          <div class="cl"></div>
             </div>
    <% end %>

controller:

def index
@news=News.all
end

Upvotes: 0

Views: 788

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

<%= news.header.truncate(50) %>

See http://api.rubyonrails.org/v4.2.7/classes/String.html#method-i-truncate

Upvotes: 2

Related Questions