是否可以在下面的代码中对字母表进行排序,还是必须在控制器中执行此操作?
下面的代码显示了所有网站,但我想按字母表排序并在离线网站上排序。因此,离线网站始终处于领先地位。
<% @websites.each_with_index do |website, index| %>
假设你有像offline: true
这样的字段,name: <string>
在Website
模型中:
<% @websites.order(offline: :desc, name: :desc).each_with_index do |website, index| %>
但是,您可能会在控制器中检索@websites
,因此最好在控制器/模型范围/视图外的任何位置应用该order
条件。
<% @websites.sort.each_with_index do |website, index| %>
@websites是否是一个数组。<% @websites.order('title ASC').each_with_index do |website, index| %>
如果活动记录,这里的标题是指我们要排序的列。