I’m having trouble sorting a single-column table in Rails. Each row represents a single object (an article) and contains all of its attributes (name, content, created_at, user, etc.). The search function works fine (Article.where) but I can’t seem to sort the table by any attributes, i.e. Article.order('attribute'). The default, which I can’t change, is created_at desc. Am I overlooking something?
Here is my controller:
def index
@title="Home"
if params[:search]
@search=params[:search]
@articles=Article.where('name LIKE ? OR category LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%").paginate(:per_page => 15, :page => params[:page])
else
@articles=Article.order('name').paginate(:per_page => 15, :page => params[:page])
end
end
And view:
<table>
<%= render @articles%>
</table>
<%= will_paginate @articles, :previous_label => "Prev", :next_label => "Next" %>
Use
reorderto override any default ordering.Article.reorder('name').paginate(:per_page => 15, :page => params[:page])