Group and sort blog posts by date in Rails
I've searched all over web and have not found the answer. I'm trying to have a very standard arch开发者_开发知识库ive option for my blog based on date. A request to url blog.com/archive/2009 shows all posts in 2009, blog.com/archive/2009/11 shows all posts in November 2009 etc. I found two different of code but not very helpful to me.
def display_by_date
year = params[:year]
month = params[:month]
day = params[:day]
day = '0'+day if day && day.size == 1
@day = day
if (year && month && day)
render(:template => "blog/#{year}/#{month}/#{date}")
elsif year
render(:template => "blog/#{year}/list")
end
end
def archive
year = params[:year]
month = params[:month]
day = params[:day]
day = '0'+day if day && day.size == 1
if (year && month && day)
@posts_by_month = Blog.find(:all, :conditions => ["year is?", year])
else
@posts_by_month = Blog.find(:all).group_by { |post| post.created_at.strftime("%B") }
end
end
Any help is appreciated.
Edit: I've managed to get it to work. If you go to 'blog/2010' you see all posts made in 2010, and if you go to 'blog/2010/apr' you see all posts made in 2010 april etc.
def archive
year = params[:year]
month = params[:month]
day = params[:day]
if (year && month && day)
requested_date = Date.new(year.to_i, Date.parse(month).month.to_i, day.to_i)
from = requested_date - 1
to = requested_date + 1
@posts_by_month = Blog.find(:all, :conditions => ["due BETWEEN ? AND ?", from, to])
elsif (year && month)
requested_month = Date.new(year.to_i, Date.parse(month).month.to_i)
end_month = requested_month.end_of_month
@posts_by_month = Blog.find(:all, :conditions => ["due BETWEEN ? AND ?", requested_month, end_month])
else
requested_year = Date.new(year.to_i)
@posts_by_month = Blog.find(:all, :conditions => ["created_at BETWEEN ? AND ?", requested_year, requested_year.end_of_year ])
end
end
#routes.rb
map.connect 'blog/:year/:month/:day',
:controller => 'blogs',
:action => 'archive',
:year => /\d{4}/,
:month => /\w{3}/,
:day => /\d{2}/,
:day => nil,
:month => nil
I don't know if this is 'good' code, but I'm sure someone can make it even better. I'd appreciate it if someone did.
I updated sent-hil's answer for Rails 4. As with him, I don't know if this is "good" code but it seems to be working for me. I will update this post if I find/make any improvements.
def archive
year = params[:year]
month = params[:month]
day = params[:day]
if (year && month && day)
requested_date = Date.new(year.to_i, Date.parse(month).month.to_i, day.to_i)
from = requested_date - 1
to = requested_date + 1
@posts = Post.find_by_sql([ "SELECT * FROM posts WHERE created_at BETWEEN ? AND ?", from, to])
elsif (year && month)
requested_month = Date.new(year.to_i, Date.parse(month).month.to_i)
end_month = requested_month.end_of_month
@posts = Post.find_by_sql([ "SELECT * FROM posts WHERE created_at BETWEEN ? AND ?", requested_month, end_month ])
else
requested_year = Date.new(year.to_i)
@posts = Post.find_by_sql([ "SELECT * FROM posts WHERE created_at BETWEEN ? AND ?", requested_year, requested_year.end_of_year ])
end
end
#routes.rb
match ':controller/:action/:year/(:month)/(:day)', via: [:get],
:controller => "posts",
:action => "archive",
:year => /\d{4}/,
:month => /\w+/,
:day => /\d{2}/
Instead of blog I have used "posts" and paths are of the form www.site.com/posts/archive/2015/September/20
精彩评论