0

控制器:

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 %>

帖子#索引视图:

<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视图上放置什么来完成此操作。任何关于放在那里的想法或更好的方法来实现它都会很棒。

任何帮助表示赞赏!

4

3 回答 3

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>
于 2013-08-25T05:16:40.727 回答
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

希望这会对某人有所帮助。

于 2014-02-17T11:46:01.950 回答
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 中的month 动作以接受两个参数params[:month] & params[:year]

希望这会有所帮助

于 2013-08-25T05:40:31.920 回答