0

I followed Railscast's episode #228 to create a sortable table column in my app.

My issue: when I press on the column name first time - it shows orders in ascending order. Next time I click - it doesn't automatically show records in descending order; however, if I manually write desc: "http://localhost:3000/admin/users/2/records?direction=desc&sort=created_at" - it works perfectly; what's a problem?

My controller:

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

My 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

My view:

<tr>
  <th><%= sortable "created_at" %></th>
</tr>
Nadiya
  • 1,421
  • 4
  • 19
  • 34
  • I think you need to set these params in rails routs (located in config/routes.rb ). – Kaleem Ullah Oct 15 '15 at 11:49
  • Can you please give me an example how it should look like? – Nadiya Oct 15 '15 at 11:50
  • can you please post the url of the page that clicking on column name redirects you to? – basiam Oct 15 '15 at 11:58
  • http://localhost:3000/admin/users/2/records?direction=asc&sort=created_at – Nadiya Oct 15 '15 at 11:59
  • try this: `@posts = Post.all.paginate(:page => params[:page], :per_page => 4).order(id: :desc)` – Kaleem Ullah Oct 15 '15 at 11:59
  • all the same; shows only records in ascending order – Nadiya Oct 15 '15 at 12:02
  • and if you click column name again (so twice it total), will it work nicely in desc order? if yes, make sure SORT_DIRECTION_ASC is set correctly and equals "asc" – basiam Oct 15 '15 at 12:04
  • if I click on a column name again - the results will be still in ascending order - that's a problem – Nadiya Oct 15 '15 at 12:07
  • @Nadiya did you add `helper_method :sort_column, :sort_direction` this inside your Story controller? – Minato Oct 15 '15 at 12:51
  • I did add the helper_method – Nadiya Oct 15 '15 at 12:56
  • and can you post the link behind column anchor tag? – Minato Oct 15 '15 at 13:06
  • @basia I saw you helped another guy with a similar problem (http://stackoverflow.com/questions/28904439/ruby-on-rails-table-not-sorting-properly). Please help me too :) – Nadiya Oct 15 '15 at 13:49
  • @Nadiya I'm pretty sure the problem caused by `column == sort_column && sort_direction == "asc"` always being equal false for some reason (causing `direction` to be always `asc` in column name link), but no idea why since the code looks good to me :( – basiam Oct 15 '15 at 15:57

1 Answers1

1

I had to write:

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

instead of:

direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
Nadiya
  • 1,421
  • 4
  • 19
  • 34