Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm pretty sure someone can make it much better than this or if it's the best way of doing it, but here's some things you could do. You can extract the middle part into a helper method. </p> <pre><code>def archive(posts) &lt;% posts.each do |post| %&gt; &lt;% archive_array = [] %&gt; &lt;% date = post.created_at.strftime("%m") + " " + post.created_at.strftime("%Y") %&gt; &lt;% if !archive_array.include? date %&gt; &lt;% archive_array &lt;&lt; date %&gt; &lt;% end %&gt; &lt;% end %&gt; end </code></pre> <p>So now your view would look like this.</p> <pre><code>&lt;h4&gt;Archive&lt;/h4&gt; &lt;%# It's probably still awful %&gt; &lt;% @posts = BlogNgin::Post.order('created_at DESC') %&gt; &lt;% archive_array = archive(@posts) %&gt; &lt;% archive_array.each do |date| %&gt; &lt;% date = date.split(' ') %&gt; &lt;%= link_to Date::MONTHNAMES[date[0].to_i].to_s + " " + date[1], blog_ngin.root_path + date[1] + '/' + date[0] %&gt;&lt;br /&gt; &lt;% end %&gt; </code></pre> <p>You can remove this line <code>&lt;% @posts = BlogNgin::Post.order('created_at DESC') %&gt;</code> and set it in your controller action as <code>@posts = BlogNgin::Post.order('created_at DESC')</code> Not sure if you can change this into a scope to do something like @posts = <code>BlogNgin::Post.desc</code></p> <p>You can also shift the last part to another helper method as follows I'm not too sure if you can directly use the link_to method in the helper file but I think it should work. </p> <pre><code>def links(archive_array) MONTHNAMEs = #put your array here &lt;% archive_array.each do |date| %&gt; &lt;% date = date.split(' ') %&gt; &lt;%= link_to Date::MONTHNAMES[date[0].to_i].to_s + " " + date[1], blog_ngin.root_path + date[1] + '/' + date[0] %&gt; &lt;% end %&gt; end </code></pre> <p>So your view would look this (hopefully)</p> <pre><code>&lt;h4&gt;Archive&lt;/h4&gt; &lt;%# It's probably still awful %&gt; &lt;%= links(archive(@posts)) %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload