简体   繁体   中英

Generating an archive of blog posts from list of posts in Ruby

I'm creating a blog using Ruby on Rails for my site. I really like the way some blogging platforms have an archive of all the posts on the blog as seen here

在此处输入图片说明

I'm trying to write code to generate that in Ruby. I have a rough idea as seen by my code

<h6>Archive</h6>
<%
    @posts.each do |post|
        #Add dropdown for post year if first entry into year
        #Add dropdown for post month if first entry into month
        #Add post to dropdown of month under dropdown of year
    end
%>

However, I'm having a hard time translating that into ERB code as I'm still fairly new to Ruby. Can someone give me an example of this being done? Thanks!

EDIT: The list @posts is sorted newest to oldest

You can use Ruby's group_by method. That will allow you put them into buckets using the created_at field.

Edit: This rails cast explains it very well: http://railscasts.com/episodes/29-group-by-month?view=asciicast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM