我有一个表,可以对消息进行排序,以及消息何时发送。现在,当用户进入页面时,它从排序消息体开始,我希望它从用户启动页面时发送的时间desc开始。我会展示一些代码和截图。我还在railscast #228上找到了这个代码。
下面是对它们进行排序的方法。
def sort_column
Message.column_names.include?(params[:sort]) ? params[:sort] : "body"
end
def sort_direction
%w(asc desc).include?(params[:direction]) ? params[:direction] : "asc"
end这里是风景
<tbody>
<% @message.each do |message| %>
<tr>
<td><%= message.body %></td>
<td><%= time_ago_in_words message.created_at %> ago</td>
<td><%= message.groups.order(:id).pluck(:name).to_sentence %></td>
</tr>
<% end %>
</tbody>这是用户打开页面时所看到的,就像您可以看到消息上的箭头一样。我希望箭头在“发送”方向上。

我不知道这是不是足够的信息,但让我知道!
发布于 2016-04-04 19:15:44
sort_column和sort_direction函数默认为"body"和"asc"。
如果要将默认排序顺序默认为created_at,请将以下函数更改如下:
def sort_column
Message.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w(asc desc).include?(params[:direction]) ? params[:direction] : "desc"
end发布于 2016-04-04 19:14:24
如果您只想按创建日期显示消息,请按以下的索引操作将它们拉进来:
Message.order(created_at: :desc)https://stackoverflow.com/questions/36410838
复制相似问题