Railscast #228 可排序 Table 列:不改变方向

Railscast #228 Sortable Table Columns: doesn't change a direction

我按照 Railscast 的第 228 集在我的应用程序中创建了一个 sortable table 列。

我的问题:当我第一次按列名称时 - 它按升序显示订单。下次我点击 - 它不会自动按降序显示记录;但是,如果我手动编写 desc: "http://localhost:3000/admin/users/2/records?direction=desc&sort=created_at" - 它工作得很好;有什么问题吗?

我的控制器:

def records
    @records = @user.records.paginate(page: params[:page], per_page: 20).order(sort_column + " " + sort_direction)
  end

private
  def sort_column
    Record.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
  end

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

我的application_helper:

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

我的看法:

<tr>
  <th><%= sortable "created_at" %></th>
</tr>

我不得不写:

direction = sort_column && sort_direction == "asc" ? "desc" : "asc"

而不是:

direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"