Quantcast
Channel: Rails: Adding a "latest post" to blog - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by siegy22 for Rails: Adding a "latest post" to blog

$
0
0

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 orderend

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  endend    

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>

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>