4

I implemented this following the Ryan Bates tutorial to sort table columns and it works great, however when I render the index page the table is already sorted by title (asc), and I would like to sort the columns only when the user click on the column header.

How could I achieve this?

Code

controller

class ProductsController < ApplicationController
  helper_method :sort_column, :sort_direction

  def index
    @products = Product.order(sort_column + " " + sort_direction)
  end

  # ...

  private

  def sort_column
    Product.column_names.include?(params[:sort]) ? params[:sort] : "name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end
end

helper_method

def sortable(column, title = nil)
  title ||= column.titleize
  css_class = column == sort_column ? "current #{sort_direction}" : nil
  direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
  link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end

index.html.erb

<tr>
  <th><%= sortable "name" %></th>
  <th><%= sortable "price" %></th>
  <th><%= sortable "released_at", "Released" %></th>
</tr>

CSS

.pretty th .current {
  padding-right: 12px;
  background-repeat: no-repeat;
  background-position: right center;
}

.pretty th .asc {
  background-image: url(/images/up_arrow.gif);
}

.pretty th .desc {
  background-image: url(/images/down_arrow.gif);
}
3
  • The sorting you describe would best be implemented in Javascript, as it is fundamentally a client-side problem (whereas rails is there to help solve the server-side problems). The table sorter plugin for jquery is exactly what you're looking for: tablesorter.com/docs. Commented Feb 3, 2013 at 1:39
  • 1
    Hi @Reck, thank you for the recommendation. I want to explore the possibility of doing this with rails, as a learning process. Commented Feb 3, 2013 at 1:47
  • What would be the default sort on the list of products that you want ?? Commented Feb 3, 2013 at 6:34

3 Answers 3

6

You should check out Ransack. It does a great job for sorting and complex searches. There is a great RailsCasts video that should help you and is much less invasive.

Sign up to request clarification or add additional context in comments.

3 Comments

As of the moment, ransack does not work with edge RoR. github.com/activerecord-hackery/polyamorous/issues/6
Try adding this to your gem file if you're having issues with polyamorous. gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
gem "ransack", github: "activerecord-hackery/ransack", branch: "rails-4.1" gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
3

Just in case someone will find this question. There is a a great gem for sortable columns (and not only that): https://github.com/leikind/wice_grid

Just look at the examples: http://wicegrid.herokuapp.com/

2 Comments

The gem is not actively maintained anymore. The author mentions that he won't work on it and it's looking for maintainers.
There's a Rails 5 fork of it. So if you're worried about using it not being maintained, the lack of maintenance by the original author is no longer a major concern.
1

You can try placing an if check on the index method

def index
  if sort_column and sort_direction
    @products = Product.order(sort_column + " " + sort_direction)
  else
    @products = Product.all()
  end
end

def sort_column
  Product.column_names.include?(params[:sort]) ? params[:sort] : nil
end

def sort_direction
  %w[asc desc].include?(params[:direction]) ? params[:direction] : nil
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.