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

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

$
0
0
  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.


Viewing all articles
Browse latest Browse all 3

Trending Articles



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