我正在尝试为我的博客存档创建一个侧边栏,列出我的博客条目的所有月份,因此当您单击“2007 年 6 月”等链接时,将加载 6 月 7 日的所有博客。 这是我的链接
<%= link_to month.first.strftime("%B %Y"), blog_archive_month_path(month.first.strftime("%Y-%m-%d")) %>
month.first 是我拉出来的记录。我的 Controller 应该看起来像这样吗?:
@blog_posts = BlogPost.where(:created_at.strftime("%Y-%m-%d") => params[:date]).(:select => "title, id, slug, created_at", :order => "created_at DESC")
我希望我可以将记录的 created_by 字段转换为我可以在匹配中传递的格式,但我得到了一个未定义的方法错误
最佳答案
这个怎么样?
将链接缩小到年月:
<%= link_to month.first.strftime("%B %Y"), blog_archive_month_path(:date => month.first.strftime("%Y-%m")) %>
然后,使用范围语法获取 SQL BETWEEN:
@blog_posts = BlogPost.
where(:created_at => (params[:date].to_date..(params[:date].to_date + 1.month))).
order("created_at desc")
关于ruby - 将记录的 created_by 字段检索到 Rails3 中传递的月份和年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6260305/