控制器:
class PostsController < ApplicationController
def index
@posts = Post.published
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
def show
.
.
end
def month
@posts_by_month = Post.find(:all, :order => "created_at DESC").group_by { |post| post.created_at.strftime("%B %Y") }
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
end
帖子#个月查看:
<% @posts_by_month.each do |monthname, posts| %>
<p><%= monthname %></p>
<div>
<ul>
<% posts.each do |post| %>
<li><p><%= post.title %></p></li>
<% end %>
</ul>
</div>
<% end %>
帖子#index view:
<h1>Listing posts</h1>
<%= render :partial => @posts %>
<h2>Blog archive</h2>
<%= ?I want link to single months archive here? %>
我正在使用rails创建一个博客,我想我会在许多博客的侧边栏中添加您常见的归档部分。当我导航到posts#month
视图时,它会将月份显示为标题,并列出该月份发布的所有帖子。
我现在要做的是列出一个月份列表,这些月份会在posts#index
视图上发布,每个月与上述posts#month
视图相关联。
我不确定在posts#index
视图中放置什么来实现此目的。关于放置什么或者更好的方法来实现这一点的任何想法都会很棒。
任何帮助表示赞赏!
答案 0 :(得分:2)
我总是通过这种方式进行分组:
@posts_by_month = Post.find(:all, :order => "created_at DESC").group_by { |post| post.created_at.beginning_of_month }
然后创建例如posts/_post_archive.html.erb
:
<div id="post-archive">
<% @posts_by_month.each do |month, posts| %>
<h4><%= "#{month.strftime('%B %Y')} (#{posts.count}):" %></h4>
<ul>
<% for post in posts %>
<li><%= link_to post.title, post %></li>
<% end %>
</ul>
<% end %>
</div>
在需要的地方写下<%= render :partial => "posts/post_archive" %>
<强>更新强>
在你的控制器中创建动作:
def by_year_and_month
@posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")
end
在routes.rb
:
match 'posts/by_year_and_month/:year/:month' => 'posts#by_year_and_month', :as=> :posts_by_year_and_month
修改我们的posts/_posts_archive.html.erb
:
<h4><%= link_to "#{month.strftime('%B %Y')} (#{posts.count}):", posts_by_year_and_month_path(:year=> month.year, :month => month.month) %></h4>
答案 1 :(得分:2)
如果有人为Postgres尝试此操作,上述方法将无效。
def by_year_and_month
@posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")
end
所以你可以这样做,
def by_year_and_month
@posts = Post.where('extract(year from created_at) = ?', params[:year]).where('extract(month from created_at) = ?', params[:month]).order("created_at DESC")
end
希望这会对某人有所帮助。
答案 2 :(得分:0)
在PostsController中
def index
@posts = Post.published
@grouped_posts = Post.select("COUNT( * ) AS posts, MONTHNAME( created_at ) AS MONTH , YEAR( created_at ) AS YEAR").group("MONTH, YEAR").order("YEAR, MONTH( created_at )")
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
然后查看index.html.erb
<h1>Listing posts</h1>
<%grouped_posts.each do |rec|%>
<%=link_to "#{rec.month} #{rec.year} (#{rec.posts})", {:controller => "posts", :action => "month", :month => rec.month, :year => rec.year} %>
<%end%>
<%= render :partial => @posts %>
<h2>Blog archive</h2>
<%= ?I want link to single months archive here? %>
然后更改posts_controller中的月份操作以接受两个参数params [:month]&amp; PARAMS [:年]
希望这会有所帮助