how to group posts by month and year,archive display

11 views
Skip to first unread message

Sailatha Kashamoni

unread,
Feb 25, 2015, 2:20:35 AM2/25/15
to rubyonra...@googlegroups.com
if i click on month i want to display only that months blog posts,this
is an existing code which is working fine for few months not for
all,where am i doing wrong ,should i change the code.can any one help me
out.and also i want the month and date to display in reverse ,recent on
top.like

    ARCHIVES
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    June 2014
    May 2014
    .
    .

app/blogcontroller.rb

    class BlogController < ApplicationController

    require 'date'
    if (params[:population_categories])
      @blog_posts = BlogPost.live.where(:population_category_ids
=>params[:population_categories]).latest.page(params[:page])
     elsif (params[:month])
      b_dt = DateTime.strptime("01/" + params[:month].to_s + "/" +
params[:year] + " 00:00", "%d/%m/%Y %H:%M")
      e_dt = DateTime.strptime("01/" + (params[:month].to_i+1).to_s +
"/" + params[:year] + " 00:00", "%d/%m/%Y %H:%M")
      else
@blog_posts=BlogPost.live.keyword(params[:keyword]).latest.page(params[:page])
    end



app/views/blog/index.html.erb

    <div class="right-col">
    <h2>ARCHIVES</h2>
    <div class='posts_by_month'>

      <ul>
      <% archive_string = "" %>
      <% date_value = "" %>
      <% @posts_by_month.each do |monthname, posts| %>

        <% date_value = (Date::MONTHNAMES[monthname[0..-6].to_i]) + " "
+ (monthname[3..8]) %>
        <% archive_string = "<li><a class='filter' href='/blog?month=" +
monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value +
"</a></li>" + archive_string %>
      <% end %>
      <%= raw archive_string %>
      </ul>


    </div>
  </div>

Colin Law

unread,
Feb 25, 2015, 3:41:49 AM2/25/15
to rubyonra...@googlegroups.com
On 25 February 2015 at 07:20, Sailatha Kashamoni
<sailatha.ka...@gmail.com> wrote:
> if i click on month i want to display only that months blog posts,this
> is an existing code which is working fine for few months not for
> all,where am i doing wrong ,should i change the code.can any one help me
> out.and also i want the month and date to display in reverse ,recent on
> top.like

Firstly do you understand how the existing code works? If not then
the first task is to understand it. If necessary put some debug code
in to display or log variables so you can work out what is going on.
Once you understand how the code works then again look in the log file
and use debug techniques to run through with a month that is not
working and see why it does not work. Almost certainly you will then
see the problem for yourself, if not then come back here show us the
couple of lines of code that does not do as you expect and no doubt
someone will help.

Colin
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/4cc19c8c-e55d-4708-9803-82e5df3de28c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages