Reputation: 1276
I have a Ruby on Rails page that I am trying to set up with sortable headers on 4 columns. I used http://railscasts.com/episodes/228-sortable-table-columns as a guide. I'm sure I'm missing something small, but the sorting works completely on the grade and entered fields as they are not in associated tables. However, when I try to sort on the user and dept fields, it only sorts ascending. The sorting works when I manually type in "desc" in the url and send that parameter, but the conditional statements don't seem to work for the employee and department fields. Also, when I click on the user or dept header, it shows the sorting arrow next to "created_at", so it seems to be defaulting some things in the helper function but I can't find out why it's doing that. Anyone see anything wrong in the code?
Index.html.erb
<table class="table table-striped table-hover">
<thead>
<tr>
<th><%= sortable "user", "Employee" %></th>
<th><%= sortable "dept", "Department" %></th>
<th><%= sortable "grade", "Grade" %></th>
<th><%= sortable "created_at", "Entered" %></th>
</tr>
</thead>
<tbody>
<% @transactions.each do |transaction| %>
<tr>
<td><%= link_to transaction.user.fl_name, transaction, :target => '_blank' %></td>
<td><%= transaction.user.department.name %></td>
<td><span class="<%= 'text-success bold' if transaction.grade >=
90 %>"><%= transaction.grade %></span></td>
<td><%= transaction.created_at.strftime("%b %d, %Y %l:%M %P") %></td>
</tr>
<% end %>
</tbody>
</table>
Controller
helper_method :sort_column, :sort_direction
def index
if (has_option("edit_option") == true) || (current_user.is_admin?)
if params[:sort] == "user"
@transactions = sort_by_user
elsif params[:sort] == "dept"
@transactions = sort_by_dept
else
@transactions = sort
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @transactions }
end
else
redirect_to root_path
end
end
def sort
transaction.order(sort_column + " " + sort_direction).page(params[:page])
end
def sort_by_user
transaction.paginate(
:page => params[:page],
:include => :user,
:order => "users.first_name #{sort_direction}")
end
def sort_by_dept
transaction.paginate(
:page => params[:page],
:include => :user,
:order => "users.department_name #{sort_direction}")
end
def sort_column
transaction.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
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
Upvotes: 1
Views: 1106
Reputation: 1276
Looks like in the sort_column method I needed this as it was not finding the columns in the user table:
def sort_column
(( Transaction.column_names.include?(params[:sort]) ) ||
( User.column_names.include?(params[:sort]) )) ? params[:sort] : "created_at"
end
Upvotes: 0
Reputation: 1567
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
This condition is always false because of first part column == sort_column
(the same happens for css_class
as well). sort_column
checks if dept
/user
is one of the transaction.column_names
and since it isn't, it returns default created_at
, causing your conditions to always be evaluated to false and return asc
. All you have to do is change sort_column
, so it works for transaction.column_names
and user
/dept
as well. Or just
def sort_column
['user', 'dept', 'created_at', 'grade'].include?(params[:sort]) ? params[:sort] : "created_at
end
Upvotes: 2