need help to code a blog archive on click month and year .it should display that months post

i am try to change existing code,i am not unserstanding code,it is written for blog archive clicking on each month and year in right column it should redirect to that clicked month.by the below code only few are working not all months,december is giving invalid date error        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>

i am try to change existing code,i am not unserstanding code,it is written for blog archive clicking on each month and year in right column it should redirect to that clicked month.by the below code only few are working not all months,december is giving invalid date error

The confusing bit for me in the below code is monthname - its clearly not the name of the month because it's used both as an index into an array of month names and to extract year information - what does it contain?

What happens precisely when you click on December (what parameters are sent with the request and what is the code that raises an exception?)

Fred

My app/controllers/blog_controller.rb:

class BlogController < ApplicationController   require 'date'

  caches_page :index, :show

  def index           if (params[:population_categories])       @blog_posts = BlogPost.live.where(:population_category_ids => params[:population_categories]).latest.page(params[:page])       # @blog_posts = BlogPost.find(:all, :conditions => ['created_at > ? AND created_at < ?', b_dt.strftime("%Y-%m-%d %H:%M"), e_dt.strftime("%Y-%m-%d %H:%M")]).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")

      @blog_posts = BlogPost.live.where(:created_at => b_dt.strftime("%Y-%m-%d %H:%M DESC")..e_dt.strftime("%Y-%m-%d %H:%M DESC") ).latest.page(params[:page] )

  #@blog_posts = @posts.group_by { |t| t.publication_date .beginning_of_month }     else       @blog_posts = BlogPost.live.keyword(params[:keyword]).latest.page(params[:page])     end @posts_by_month = BlogPost.all.group_by { |post| post.created_at.strftime("%m %Y") }       end   def show     @blog_post = BlogPost.where(:url_fragment => params[:url_fragment]).first     raise ActionController::RoutingError.new('Not Found') if @blog_post.nil?     @blog_posts = BlogPost.live.keyword(params[:keyword]).latest.page(params[:page])   @posts_by_month = BlogPost.all.group_by { |post| post.created_at.strftime("%m %Y") }   end   def feed     @posts_feed = BlogPost.where(:content_state => 'live')     respond_to do |format|       format.atom { render layout: false }     end   end end

app/controllers/month_posts_controler:

class MonthPostsController < ApplicationController   def display   @posts_by_month = BlogPost.all.group_by { |post| post.created_at.strftime("%B") }   #@posts_by_month = BlogPost.all.each { |post| post.created_at.strftime("December") }

  end   def show   @posts = BlogPost.all.find(params[:id])   @posts = Post.includes(:comments).order("created_at DESC").limit(5) end end

app/controllers/post_controller: def index     @posts = Post.all

    respond_to do |format|       format.html # index.html.erb       format.json { render json: @posts }       format.atom     end end

app/views/blog/index.html.erb <div class='section white' id='blog_posts'>   <div class='section_content'>     <div class='section-left'>

      <% @blog_posts.limit(3).each do |post| %>         <% @section_keywords += post.keywords_array %>         <%= render :partial => '/blog/index/blog_post', :locals => {:atom => post} %>       <% end %>

    <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>     <div class="right-col">       <div class="blogpost-rss">         <a class="rss-link" href="/feed.atom"></a>         <%= auto_discovery_link_tag(:rss, feed_url(:format => :atom)) %>

      </div>     </div>

app/views/blog/show.html.erb: <div class='section white blog_post blog_detail' id='blog_post'>

  <div class='section_content'>          <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]) %>   <% if(date_value == pub_date) %>   <% archive_string = "<li class='active'><a class='filter' href='/blog?month=" + monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value + "</a></li>" + archive_string %>         <% else %>     <% archive_string = "<li><a class='filter' href='/blog?month=" + monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value + "</a></li>" + archive_string %>         <% end %>         <% end %>       <%= raw archive_string %>       </ul>

      </div>       </div>

My app/controllers/blog_controller.rb:

To be honest, I don’t think many people (certainly not me) want to read though a wall of code to work out which bits are relevant and which aren’t. It’s up to you as question asker to do extract from your application what is relevant and what isn’t. It would however be a lot clearer if you split out some of this into helpers rather than having it all inline in your view

Fred

can u help me understand this code <a class='filter' href='/blog?month=" + monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value + "</a>

the numbers and all

can u help me understand this code

<a class=‘filter’

href='/blog?month=" + monthname[0…-6] + “&year=” + monthname[3…8] +

“'>” + date_value + "

the numbers and all

This is the String# method Class: String (Ruby 2.2.0)

monthname[0…-6] returns the string from the first character until the one 6 from the end (negative indexes count from the end of strings)

monthname[3…8] returns from index 3 (i.e. the 4th character) until the one at index 8

So if your string is “06 2015” then monthname[0…-6] is “06” and monthname[3…8] returns “2015” (given the length of the string monthname[3…6] would return the same string)

Using strings that have to be torn apart like this does seem a little bit obtuse though.

Fred