在 Rails 中创建博客存档 [英] Creating blog archive in rails
问题描述
控制器:
class PostsController <应用控制器定义索引@posts = Post.publishedresponse_to do |格式|format.html # index.html.erbformat.json { 渲染 json: @posts }结尾结尾高清秀..结尾定义月@posts_by_month = Post.find(:all, :order => "created_at DESC").group_by { |post|post.created_at.strftime("%B %Y") }response_to do |格式|format.html # index.html.erbformat.json { 渲染 json: @posts }结尾结尾结尾
posts#month 查看:
<% @posts_by_month.each do |monthname, posts|%><p><%=月名%></p><div><ul><% posts.each 做 |post|%><li><p><%= post.title%></p></li><%结束%>
<%结束%>
posts#index 视图:
发布帖子
<%=渲染:部分=>@posts %><h2>博客存档</h2><%= ?我想要链接到这里的单月档案?%>
我正在 Rails 中创建一个博客,我想我会添加一个存档部分,您通常会在许多博客的侧边栏中看到该部分.当我导航到 posts#month
视图时,它会将月份显示为标题并列出该月内发布的所有帖子.
我现在想要做的是在 posts#index
视图上发布的月份列表,每个月都链接到 posts#month
视图描述
我不确定要在 posts#index
视图上放什么来完成此操作.任何关于将什么放在那里或更好的实现方式的想法都会很棒.
感谢任何帮助!
万一有人在 Postgres 上尝试这个,上述方法将不起作用.
def by_year_and_month@posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")结尾
所以你可以这样做,
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")结尾
希望这会对某人有所帮助.
Controller:
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#month View:
<% @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 %>
posts#index view:
<h1>Listing posts</h1>
<%= render :partial => @posts %>
<h2>Blog archive</h2>
<%= ?I want link to single months archive here? %>
I'm creating a blog in rails and I thought I would add an archive section that you commonly see in the sidebar of many blogs. When I navigate to the posts#month
view it displays the month as a heading and lists all the posts made during that month.
What I want to do now is have a list of months that posts where made on the posts#index
view with each month linked to the posts#month
view described above.
I'm not sure what to put on the posts#index
view to accomplish this. Any ideas on what to put there or a better way to implement this would be great.
Any help appreciated!
In case someone try this for Postgres, the above method will not work.
def by_year_and_month
@posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")
end
so you can do it like this,
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
Hope this will help someone.
这篇关于在 Rails 中创建博客存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!