group posts by month,archive display

43 views
Skip to first unread message

Sailatha K.

unread,
Feb 25, 2015, 2:23:34 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>

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Feb 25, 2015, 3:42:57 AM2/25/15
to rubyonra...@googlegroups.com
I have responded to this in the earlier thread where you asked it the
first time.

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/cff90cd1d52510d4fc63b5e660235377%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Sailatha K.

unread,
Feb 25, 2015, 6:03:02 AM2/25/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1168627:
> I have responded to this in the earlier thread where you asked it the
> first time.
>
> Colin

i am working on the existing code,i am new to ruby on rails ,i am not
understanding this code and how is it working.why is 0..-6 given in
month and 3...8 in year.

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

Colin Law

unread,
Feb 25, 2015, 6:19:43 AM2/25/15
to rubyonra...@googlegroups.com
On 25 February 2015 at 11:01, Sailatha K. <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1168627:
>> I have responded to this in the earlier thread where you asked it the
>> first time.
>>
>> Colin
>
> i am working on the existing code,i am new to ruby on rails ,i am not
> understanding this code and how is it working.why is 0..-6 given in
> month and 3...8 in year.

In that case I suggest you take a few days out to work right through a
tutorial on rails, such as railstutorial.org, which is free to use
online. That will give you a basic understanding of rails, and you
will recover the time spent very rapidly.

To answer the question you first need to know what is in monthname, I
suggest first you need to know what is in monthname. If you include
something like
<%= "monthname = #{monthname}" %>
that will display whatever is in monthname. Next look at the docs for
the String (I assume that monthname contains a string) to see what
String[0..-6] and [3..8] do and to check if you
<%= "[monthname[0..-6]" "(monthname[3..8]" %>
for the cases that work and those that don't it will hopefully give
you some idea.
The docs for Range will tell you what [0..-6] do. Your code does look
a little odd but I don't know what is in monthname, and the fact that
it sometimes works means there must be some useful code there.

Colin

>
> <% 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 %>
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> 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/ab1736a906078607af4a4661d8a31e6b%40ruby-forum.com.

Sailatha K.

unread,
Feb 27, 2015, 7:30:50 AM2/27/15
to rubyonra...@googlegroups.com
okay thank you,i will try my best
Reply all
Reply to author
Forward
0 new messages