I am having trouble displaying information from my database in Rails. I am able to display every row on one page, but I cannot limit the display to just 5 rows of data. In my code, Article is a model with a text field and a title field. I am using rails 4.1.8 and Ruby 2.1.5.
The code I am using:
<% posts = Article.all.order('created_at DESC'%>
<% posts.find_each(batch_size: 5) do |post| %>
<li>Title: <% post.title %></li>
<li>Date: <% post.created_at.strftime("%d %b. %Y") %></li>
<li>Body: <% post.text %></li>
<% end %>
And the output I get from it is:
<li>Title: </li>
<li>Date: </li>
<li>Body: </li>
<li>Title: </li>
<li>Date: </li>
<li>Body: </li>
The reason there are two sets here is because I currently only have two articles in the database, which at least shows that the database is being accessed here, but using post.title does not actually return any text.
As part of a tutorial, I used this code:
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article.created_at.strftime("%d %b. %Y") %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
This code does exactly what I expect, but of course it displays every row in the database, which would be overwhelming for a large number of articles.
Hopefully this is a really easy question and I am just making an obvious mistake... This is my first time using Ruby or Rails (I am doing this in attempt to learn for a school project).
Thank you for the help!