Is there an elegant way to implement a sort asc and desc actions in the views/controller in rails?
What I have is the common index.html.erb view that lists all of my
Here are two examples that I'm using. The first one with @plans
is where I just want to order a decimal from lowest to highest. The other example is a bit more complicated where I want to order user files from newest to oldest. I then used a second variable to group the files by the date created. Both examples where performed in the controllers.
@plans = Plan.order("price")
@files= @user.files.order("id DESC").all
@dates = @files.group_by { |t| t.created_at }
For clickable buttons in your view, check out Ryan Bates's Railscast episode covering this information.
http://railscasts.com/episodes/228-sortable-table-columns
here is relation of data that shows you how we access name in acceding order
@results = Result.has_pro.ascending(:name).page(params[:page])
I recently used a gem called sorted
with good results in case you want to use a pre-packaged solution for this instead of rolling out your own.
https://github.com/mynameisrufus/sorted