GVS
GVS

Reputation: 229

Rails: Adding a "latest post" to blog


Adding a "latest post" section to blog

This is my original code to show all my posts and it works great.

<% @post.each do |post| %>
  <div class="col-md-4 col-sm-6 portfolio-item">
    <a href="#portfolioModal6" class="portfolio-link" data-toggle="modal">
      <div class="portfolio-caption">
        <h4><%= link_to post.title, post %></h4>
        <p class="text-muted"><%= post.created_at.strftime("%B, %d, %Y") %></p>
      </div>
    </a>
  </div>
<% end %>

However, I also want to add a large div that shows my latest post. So I copy/paste the code above and changed the line <% @post.each do |post| %>to the following:

<% @post = Post.last do |post| %>

There was no errors, but nothing showed up either. The reason I added this code is because I saw in a tutorial where the teacher went into rails c and typed @post = Post.last and could see the last post created. However, when I go into terminal and type it, I get the error:

No command '@post' found, did you mean:
 Command 'mpost' from package 'texlive-binaries' (main)
 Command 'rpost' from package 'suck' (universe)
@post: command not found

Upvotes: 0

Views: 51

Answers (2)

siegy22
siegy22

Reputation: 4413

Extending @oreoluwa's answer with a little more rails conventions:

Your post object:

class Post < ActiverRecord::Base
  scope :ordered, -> { order(created_at: :desc) } # or use your own column to order
end

Your Controller (where your view is where the last post should be rendered). You should not make queries in your views to keep better a better control.

class ExamplesController < ApplicationController

  def show
    @latest_post = Post.ordered.first
  end

end    

Your View

<div class="col-md-4 col-sm-6 portfolio-item">
  <a href="#portfolioModal6" class="portfolio-link" data-toggle="modal">
    <div class="portfolio-caption">
      <h4><%= link_to @latest_post.title, @latest_post %></h4>
      <p class="text-muted"><%= @latest_post.created_at.strftime("%B, %d, %Y") %></p>
    </div>
  </a>
</div>

Upvotes: 1

oreoluwa
oreoluwa

Reputation: 5623

  1. You shouldn't type it on your terminal, but in the rails console. On your terminal, type rails console, when that loads up, you can then do:

    post = Post.last
    
  2. In order to only render for the last post, you wouldn't need the block, as such:

        <% post = Post.last %>
        <div class="col-md-4 col-sm-6 portfolio-item">
          <a href="#portfolioModal6" class="portfolio-link" data-toggle="modal">
            <div class="portfolio-caption">
              <h4><%= link_to post.title, post %></h4>
              <p class="text-muted"><%= post.created_at.strftime("%B, %d, %Y") %></p>
            </div>
          </a>
        </div>
    

By the way, it's not best practice to handle domain objects in your view. You could have this as an instance variable in your controller, as a Facade object if you have multiple objects.

Upvotes: 3

Related Questions