Middleman App, sort blog posts by month - middleman

I'm using middlemanapp to create a blog. I'm trying to output an archive of blog posts sorted by month and year to display in a sidebar. eg. April 2010, May 2010, June 2010, with clickable links to a archive.
So far have this code below which will output the month in number form (eg. July is being output as 7) and I need to have a list that is displayed by month as shown above.
<% blog.articles.group_by {|a| a.date.month }.each do |month, articles| %>
<li><%= link_to month, blog_year_path(month) %> </a></li>
<% end %>
Can someone help, I'm even not certain if middleman offers this functionality, but I'm not very familiar with ruby.

I couldn't find an easy built-in way to do this with Middleman either, but the following will give you a nested list of years and months, with the relevant links:
<ul>
<% blog.articles.group_by {|y| y.date.year }.each do |year, articles| %>
<li>
<a href="<%= blog_year_path(year) %>">
<%= year %>
</a>
<ul>
<% articles.group_by {|a| a.date.month}.each do |month, month_articles| %>
<li><%= link_to month_articles.first.date.strftime("%B"), blog_month_path(year, month) %></li>
<% end %>
</ul>
<% end %>
</li>
</ul>
e.g.
2013
August
July
June
...
(I'm fairly sure I borrowed the above from this Middleman template on Github, but if not it was found via a Github search for "blog.articles.group_by month".)

Related

Trying to remove the ID, timestamps and other information below the titles

enter image description here
I only want the titles of the book and not the text below it. I tried looking if I am displaying anything other than the title but I can't seem to find it.
This is my index.html.erb file.
<%= #books.each do |book| %>
<h2><%= book.title %></h2>
<% end %>

Link_to helper: Nested Routes

This driving me crazy...
I've built some test apps with standard routing, but this time wanted to nest b within a. So like this (a and b are examples for ease)
resources :a do
resources :b
end
in the 'b controller' I have index defined as
def index
#b = B.all
end
On the index page I have
<p>B index</p>
<ul class="b">
<% #b.each do |b| %>
<li>
<%= link_to b.name, b %>
</li>
<% end %>
Controller params are set
def B_params
params.require(:b).permit(:name, :description, :this, :that, :a_id)
end
So the error message is when viewing the index.html 'undefined method for b_path'
I'm pretty sure its a routing problem as all of my other index pages work fine. So what is the correct way to route the nested b_path.
I have tried
get '/a/:id/b', to: 'b#index' -----fails
get '/a/:a_id/b', to: 'b#index' ----fails
get '/b', to: 'b#index' ----- not surprising fails
So what am I missing. Is there something else to do or another way of linking once you nest resources
Thanks
OK so I managed to figure this out after what feels like an eternity. Just in case anyone is interested
The fault lied with the reference to 'b' immediately after the link_to b.name statement. It should have been...
<p>B index</p>
<ul class="b">
<% #b.each do |b| %>
<li>
<%= link_to b.name, a_b %>
</li>
So to explain, as I understand it. The three important files once nested are routes.rb where you need to set the correct route for the index... for this example it would be
get '/a/:a_id/b', to: 'b#index'
this comes from running rails routes and seeing the correct path returned there.
Then in the 'b controller' you declare the variable as normal
def index
#b = B.all
end
then in the view/b/index.html.erb you call the variable, but crucially refer to the new b#index path
<% #b.each do |b| %>
<li>
<%= link_to #b.name, a_b_url %>
</li>
<% end %>
I'm sure my understanding is not as complete as it could be yet but i've got it working so onwards and upwards. Happy days

RoR: why arent some of my microposts columns showing?

Here is the relevant part of the micropost view. .
<li>
<span class="content"><%= micropost.content %></span>
<span class="product"><%= micropost.product %></span>
<span class="price"><%= micropost.price %></span>
<span class="location"><%= micropost.location %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
I recently added product, price, and location to the columns in the micropost table. I checked in the database and these aspects are being recorded/written but for some reason content is the only one that gets displayed on the app. Why are the other ones left out?
Here is the generated HTML. Im assuming that this is the relevant section. as you can see the html for product, price and location is never generated.
<li id="1">
<img alt="alex" class="gravatar" src="http://gravatar.com/avatar/a6759dcef8e5aad4e77509e5479b9823.png?s=50" />
<span class="user">
alex
</span>
<span class="content">first</span>
<span class="timestamp">
Posted less than a minute ago.
</span>
delete
</li>
I dont think its the difference between attr_accessor and attr_accessible because content is attr_accessor and it shows up. also, I read the difference and it doesnt seem to be relevant here. I believe attr_accessible is the right one. plus I tried changing it to attr_accessor and it didnt work. Any other ideas? please and thank you!
Does the micropost that you are trying to see (ID = 1) have data populated on those fields (:product, :price, :location) ?
Since the generated HTML doesn't even have the span tags for those fields, the issue seems to be that Rails isn't loading the newest version of your view file. Are you running in production or in development? Have you re-started the Rails server?
Also, you are correct that attr_accessible is correct rather than attr_accessor if those are database columns.
You might also check if you have any kind of caching enabled that would be displaying a cached copy of the generated HTML. For example, you could change the content value in the database and see if that is reflected in the generated HTML.

How to make a search results page with pg_search multisearch that has links to the results?

I finally figured out how to implement pg_search's multisearch feature. But I'm having trouble making a usable search results page that displays links back to the various articles and faqs that contain the search terms. It's a pretty basic setup using Rails 3.2.3:
Models:
I have an Articles and a Faqs model, both with "Title" and "Content" attributes, and this code in both models:
include PgSearch
multisearchable :against => [:title, :content]
Search Form View Code:
The search form passes everything to a controller called "results."
<%= form_tag result_index_path, method: :get do %>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "GO", name: nil %>
<% end %>
Results Controller:
class ResultController < ApplicationController
def index
#pg_search_documents = PgSearch.multisearch(params[:query])
end
end
I would like to make a search results page that displays the title of each result found, with a link back to that item. I figured out how to pull the 'class' attribute out #pg_search_documents. My thinking is to do something like this on the results page:
<ul>
<% #pg_search_documents.each do |pg_search_document| %>
<li><%= link_to pg_search_document.searchable.title, "../#{pg_search_document.searchable.class}/#{pg_search_document.searchable.id}" %></li>
<% end %>
</ul>
An example link that this code yields is: http://localhost/Article/3. If I could figure out how to downcase and pluralize "pg_search_document.searchable.class", I'd be home free. I've tried writing various methods in the model and controller, and tried writing a helper. But this is where my Rails skills fail me.
Any suggestions? Anybody know of a more elegant way of accomplishing this? Any ideas / suggestions are very much appreciated.
I did something similar and just used
<%= link_to pg_search_document.searchable.title, pg_search_document.searchable %>
to let Rails dynamically create the path to the associated record.
Well, it's amazing what walking away from the problem for a little while does. That, and more persistent Googling on basic Ruby. Here's the solution I came up with:
<ul>
<% #pg_search_documents.each do |pg_search_document| %>
<li><%= link_to pg_search_document.searchable.title, "../#{(pg_search_document.searchable.class).to_s.downcase!.pluralize}/#{pg_search_document.searchable.id}" %></li>
<% end %>
</ul>
Still, this seems ugly to me. I'd still be very interested to see something more streamlined and intelligent.

Allowing a Post to show datetime without user input (Ruby on Rails)

I want to be able to show a datetime stamp ( I want it to appear like - "Posted 5 days ago" ), but i dont want the date field to appear when an user is making a post.
I just want to post to add it automatically. Any help? (Im just learning Rails so its all new to me...)
Thanks in advance.
For a Forum Application I did this:
<td class="right">
<% if forum.most_recent_post %><%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %> ago by <%= link_to forum.most_recent_post.user.login, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %> no posts <% end %>
</td>
Here <%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %> ago by "username" so, you may try this. I think that can work in this scenario too. Just use distance_of_time_in_words_to_now post.time here post.time is the time when user made a post.