Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中ruby中按日期降序排序_Ruby On Rails_Ruby_Sorting - Fatal编程技术网

Ruby on rails rails中ruby中按日期降序排序

Ruby on rails rails中ruby中按日期降序排序,ruby-on-rails,ruby,sorting,Ruby On Rails,Ruby,Sorting,我想按日期降序排列我的列表,作为“新用户列表”,在数据库中我有一列 t.datetime "created_at", null: false 这是新用户注册的时间,在视图中,我有如下代码: %table.table.table-striped.table-hover %thead %h3 New Users %hr %th Name %th Comp

我想按日期降序排列我的列表,作为“新用户列表”,在数据库中我有一列

t.datetime "created_at",                                      null: false  
这是新用户注册的时间,在视图中,我有如下代码:

%table.table.table-striped.table-hover
      %thead
      %h3 New Users
      %hr
        %th Name
        %th Company
        %th Role
        %th Created date
        %th{:width => '50'}
        %th{:width => '50'}
        %th{:width => '50'}
      %tbody
      - @users.each do |user|
        -if user.role == "trial-member"
          - @created_at.sort{|a,b| b.created_at <=> a.created_at}.each do |created_at|
            %tr
            %td
              = user.first_name
              = user.last_name
            %td= user.company
            %td= user.role
            %td= user.created_at
            %td= link_to 'Approve', edit_user_path(user),  {:class => 'btn btn-success btn-sm'}
%table.table.table-striped.table-hover
%泰德
%h3新用户
%人力资源
%第名
%th公司
%第四角色
%创建日期
%th{:width=>'50'}
%th{:width=>'50'}
%th{:width=>'50'}
%t车身
-@users.each do | user|
-如果user.role==“试用会员”
-@created_at.sort{a,b|b.created_at a.created_at}。每个do|created_at|
%tr
%运输署
=user.first\u name
=user.last_name
%td=用户公司
%td=user.role
%td=用户创建的\u地址
%td=链接到“批准”,编辑用户路径(用户),{:class=>“btn btn成功btn sm”}
但这会给出一个错误,即“nil:NilClass的未定义方法'sort'”,我应该如何按创建日期对表中的列表进行降序排序?谢谢。

在控制器中:

@users = User.order('created_at DESC')
只需在获取
@users
的逻辑中添加:
order('created_at DESC')

在您看来,您现在可以摆脱
-@created_at.sort{a,b | b.created_at a.created_at}。每个

%h3 New Users
%table.table.table-striped.table-hover
  %thead
    %tr
      %th Name
      %th Company
      %th Role
      %th Created date
      %th{:width => '50'}
      %th{:width => '50'}
      %th{:width => '50'}
  %tbody
    - @users.each do |user|
      -if user.role == "trial-member"
        %tr
          %td
            = user.first_name
            = user.last_name
          %td= user.company
          %td= user.role
          %td= user.created_at
          %td= link_to 'Approve', edit_user_path(user),  {:class => 'btn btn-success btn-sm'}

您看到的错误是因为
@created_at
不是可枚举对象,因此它不响应
排序

这是因为
@created_at
未在任何位置定义。因此,任何未定义的实例变量在默认情况下都返回nil。如果要按排序顺序显示用户,则需要按顺序获取
@users

@users = User.order(created_at: :desc)

对于那些对这个问题投了否决票的人,请向提出问题的人提供可操作的反馈,以便他们能够改进。让我们帮助我们的社区变得更棒!您还可以使用
User.order(在::desc创建)