Sign in through GitHub

RailsCasts Pro episodes are now free!

Learn more or hide this

Steve C.'s Profile

GitHub User: steve981cr

Comments by Steve C.


Below is the code updated for Rails 4.2. Note: the Rails api says that the group_by method is deprecated in Rails, but don't be confused by that, they just mean that you use the Ruby group_by method instead of the Rails' modified version (which no longer exists).

def index
  @tasks = Task.order(:due_at)
  @task_months = @tasks.group_by { |t| t.due_at.beginning_of_month }
<% @task_months.each do |month, tasks| %>
  <h2><%= month.strftime('%B') %></h2>
  <% tasks.each do |task| %>
    <div class="task">
      <strong><%= %></strong>
      due on <%= task.due_at.to_date.to_s(:long) %>
  <% end %>
<% end %>

This works as is on Rails 4.2 except for the CSS. Combining other people's advice above the below works for me. Remove .pretty and add a. Change the url from the image folder to the assets folder.

th a.current {
  padding-right: 20px;
  background-repeat: no-repeat;
  background-position: right center;
th a.current.asc {
  background-image: url(/assets/up_arrow.gif);
th a.current.desc {
  background-image: url(/assets/down_arrow.gif);

You can also use Bootstrap 3 glyphicons instead of background images. Replace the helper method with this and leave out the css classes.

def sortable(column, title = nil)
  title ||= column.titleize
  if column != sort_column
    css_class = nil
  elsif sort_direction == "asc"
    css_class = "glyphicon glyphicon-triangle-top"
    css_class = "glyphicon glyphicon-triangle-bottom"
  direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
  link_to "#{title} <span class='#{css_class}'></span>".html_safe, {:sort => column, :direction => direction}

This also works:

self.category = Category.find_or_create_by(name: name) if name.present?