Creating a blog on rails

Controller:

class PostsController < ApplicationController
 def index
  @posts = Post.published    

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

  def show    
  .
  .
  end

  def month
    @posts_by_month = Post.find(:all, :order => "created_at DESC").group_by { |post| post.created_at.strftime("%B %Y") }

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

Messages # month View:

<% @posts_by_month.each do |monthname, posts| %>
<p><%= monthname %></p>
<div>
    <ul>
        <% posts.each do |post| %>
            <li><p><%= post.title %></p></li>
        <% end %>
    </ul>
</div>

<% end %>

posts # index view:

<h1>Listing posts</h1>

<%= render :partial => @posts %>

<h2>Blog archive</h2>
<%= ?I want link to single months archive here? %>

I create a blog on rails, and I thought I would add the archive section that you usually see in the sidebar of many blogs. When I go to the view posts#month, it displays the month as a title and lists all the messages made during that month.

Now I want to make a list of the months in which the records that were made in the view posts#indexwith each month associated with the view posts#monthdescribed above are placed .

I'm not sure what to add to the view posts#indexto accomplish this. Any ideas on what to put or the best way to implement this would be great.

Any help appreciated!

0
3

:

@posts_by_month = Post.find(:all, :order => "created_at DESC").group_by { |post| post.created_at.beginning_of_month }

, , posts/_post_archive.html.erb:

<div id="post-archive">
  <% @posts_by_month.each do |month, posts| %>
    <h4><%= "#{month.strftime('%B %Y')} (#{posts.count}):" %></h4>
   <ul>
     <% for post in posts %>
       <li><%= link_to post.title, post %></li>
     <% end %>
   </ul>
  <% end %>
</div>

<%= render :partial => "posts/post_archive" %>

UPDATE:

:

def by_year_and_month
  @posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")
end

routes.rb:

match 'posts/by_year_and_month/:year/:month' => 'posts#by_year_and_month', :as=> :posts_by_year_and_month

posts/_posts_archive.html.erb:

<h4><%= link_to "#{month.strftime('%B %Y')} (#{posts.count}):", posts_by_year_and_month_path(:year=> month.year, :month => month.month) %></h4>
+2

- Postgres, .

def by_year_and_month
  @posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")
end

, ,

def by_year_and_month
@posts = Post.where('extract(year  from created_at) = ?', params[:year]).where('extract(month  from created_at) = ?', params[:month]).order("created_at DESC")
end

, -.

+1

PostsController

def index
  @posts = Post.published    
  @grouped_posts = Post.select("COUNT( * ) AS posts, MONTHNAME( created_at ) AS MONTH , YEAR( created_at ) AS YEAR").group("MONTH, YEAR").order("YEAR, MONTH( created_at )")
  respond_to do |format|
   format.html # index.html.erb
   format.json { render json: @posts }
  end
 end

index.html.erb

<h1>Listing posts</h1>
<%grouped_posts.each do |rec|%>
<%=link_to "#{rec.month} #{rec.year} (#{rec.posts})", {:controller => "posts", :action => "month", :month => rec.month, :year => rec.year} %>
<%end%>
<%= render :partial => @posts %>

<h2>Blog archive</h2>
<%= ?I want link to single months archive here? %>

posts_controller, params [: month] params [: year]

,

0

Source: https://habr.com/ru/post/1543342/


All Articles