endritgr
endritgr

Reputation: 21

How to implement archives in blog with ruby and sinatra?

I'm doing archives for my blog ,actually I'm moving from rails to sinatra because of the requirements.I'm trying the same for Sinatra

In my app.rb:

def index
   @posts = Post.all(:select => "title, id, created_at", :order => "created_at DESC")
   @post_months = @posts.group_by { |t| t.created_at.beginning_of_month }
end

and in Layout.erb :

<div class="archives">
   <h2>Blog Archive</h2>

   <% @post_months.sort.reverse.each do |month, posts| %>
   <h3><%=h month.strftime("%B %Y") %></h3>
   <ul>
      <% for post in posts %>
      <li><%=h link_to post.title, post_path(post) %></li>
      <% end %>
  </ul>
  <% end %>

Can anyone please help me how to do it for sinatra? I'm trying the same code and I'm getting this :undefined method `sort' for nil:NilClass

Upvotes: 0

Views: 122

Answers (1)

DiegoSalazar
DiegoSalazar

Reputation: 13531

You are not using Sinatra's notation for defining actions. Instead of:

def index
  # your code...
end

You need to define actions like so:

get '/' do
  # your code...
end

You should probably read this before continuing: http://www.sinatrarb.com/intro.html

Upvotes: 0

Related Questions