Best way to implement sort asc or desc in rails

前端 未结 3 1806
粉色の甜心
粉色の甜心 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

    0 讨论(0)
  • 2020-12-14 09:02

    here is relation of data that shows you how we access name in acceding order

    @results = Result.has_pro.ascending(:name).page(params[:page])
    
    0 讨论(0)
  • 2020-12-14 09:13

    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

    0 讨论(0)
提交回复
热议问题