Best way to implement sort asc or desc in rails

前端 未结 3 1810
粉色の甜心
粉色の甜心 2020-12-14 08:32

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

3条回答
  •  隐瞒了意图╮
    2020-12-14 09:02

    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

提交回复
热议问题