0

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!

3 Answers 3

2

There is a big difference between:

<% post.title %>

and

<%= post.title %>

The former, just runs the ruby code. The latter, runs the ruby code, then outputs the result onto the screen. If you want the post title to show up, you need the one with the =

Sign up to request clarification or add additional context in comments.

1 Comment

That's very helpful to know.
1

You should be fetching the articles in your controller:

def index
  @articles = Article.order('created_at DESC').limit(5)
end

The limit(5) here is the magic that you want: limiting the result set to only the first 5 objects.

Taryn is right that you'll need to use <%= and not <% to output the values.

2 Comments

When using variables from the controller, do they always get passed into the view like that?
Only when you name them with an @ in front of them
-1

Couple of things on the find_each. http://apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_each It will ignore any order operations.

I would suggest doing the following change:

<% Article.order('created_at DESC').limit(5).each do |post| %>
<li>Title: <% post.title %></li>
<li>Date: <% post.created_at.strftime("%d %b. %Y") %></li>
<li>Body: <% post.text %></li>
<% end %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.