Ruby on rails 尝试对rails管理表中的列表排序

Ruby on rails 尝试对rails管理表中的列表排序,ruby-on-rails,Ruby On Rails,我正试图对我的每个管理表进行排序,以使它们按最新的优先顺序显示 例如,我想通过在DESC创建的_来订购供应商表-这意味着最新的供应商将显示在顶部 当前位于views/admin/suppliers/index.html.haml中: %thead %tr %th= sort_link(@search, :name, 'ID') %th= sort_link(@search, :name, 'Name') %th= sort_link(@search,

我正试图对我的每个管理表进行排序,以使它们按最新的优先顺序显示

例如,我想通过在DESC创建的_来订购供应商表-这意味着最新的供应商将显示在顶部

当前位于views/admin/suppliers/index.html.haml中:

%thead
    %tr
      %th= sort_link(@search, :name, 'ID')
      %th= sort_link(@search, :name, 'Name')
      %th= sort_link(@search, :category, 'Category')
      %th= sort_link(@search, :region, 'Region')
      %th Review Count
      %th Average rating
      %th Claimed?
      %th Owner
      %th
      %th
      %th
  %tbody
    = render @suppliers
%tr
  %td= supplier.id
  %td= supplier.name
  %td= supplier.category_name
  %td= supplier.region_name
  %td= supplier.review_count
  %td= supplier.average_overall_rating
  %td= supplier_claimed_label(supplier.owned?)
在views/admin/suppliers/_supplier.html.haml中:

%thead
    %tr
      %th= sort_link(@search, :name, 'ID')
      %th= sort_link(@search, :name, 'Name')
      %th= sort_link(@search, :category, 'Category')
      %th= sort_link(@search, :region, 'Region')
      %th Review Count
      %th Average rating
      %th Claimed?
      %th Owner
      %th
      %th
      %th
  %tbody
    = render @suppliers
%tr
  %td= supplier.id
  %td= supplier.name
  %td= supplier.category_name
  %td= supplier.region_name
  %td= supplier.review_count
  %td= supplier.average_overall_rating
  %td= supplier_claimed_label(supplier.owned?)
在my controllers/admin/suppliers_controller.rb中:

def index
    @search    = Supplier.search(params[:q], :order => params[:sort] || "created_at DESC")
    @suppliers = @search.result
  end
但这似乎不起作用

有人能帮忙吗


谢谢

除了我可以在文档中阅读的内容之外,我不确定Ransack的具体内容,但是
结果的返回是一个ActiveRecord关系,因此您可以添加额外的ActiveRecord方法,以影响您的结果,如

def index
    @search    = Supplier.search(params[:q])
    @suppliers = @search.result.order(created_at: :desc)
end

很明显,我无法测试这一点,因为我没有使用此gem的项目,但这应该可以工作。

您是否使用gem作为搜索方法?如果没有,你能展示一下它的方法定义吗,它会在供应商模型中。是的,它使用的是Ransack,很高兴它对你有用。我不知道奖金是怎么起作用的,因为我以前从来没有用奖金回答过任何问题,但我似乎没有得到奖金。对不起,它不允许我在前24小时内拿到奖金,然后我就忘了。谢谢你的提醒。。。