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”>
ARCHIVES
<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 = “
<a class=‘filter’ href=’/blog?month=”
- monthname[0…-6] + “&year=” + monthname[3…8] + “’>” + date_value +
“ ” + archive_string %>
<% end %>
<%= raw archive_string %>
On Friday, February 6, 2015 at 7:09:52 AM UTC, Ruby-Forum.com User
wrote:
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
On Friday, February 6, 2015 at 11:33:03 AM UTC, Ruby-Forum.com User
wrote:
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
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
<% @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 = “
<a class=‘filter’ href='/blog?month=”
</div>
<%= auto_discovery_link_tag(:rss, feed_url(:format => :atom)) %>
</div>
app/views/blog/show.html.erb:
ARCHIVES
<% 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 = "- " + date_value + "
" + archive_string %>
<% else %>
<% archive_string = "- " + date_value +
"
" + archive_string %>
<% end %>
<% end %>
<%= raw archive_string %>
</div>
</div>
how to write the code for blog archives,any changes are needed in above
code ?
can u help me understand this code
" + date_value + "
the numbers and all
On Friday, February 6, 2015 at 12:21:09 PM UTC, Ruby-Forum.com User
wrote:
can u help me understand this code
" + date_value + "
the numbers and all
This is the String#[] method
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