はじめに
Ruby on Railsで管理画面作っていて、Administrateを使っています。
こちらのgemの使い方はまた次回に書きます。
起きた問題
単純にモデル.all
で取得したレコードのorderを指定したかった。
ただし、すでに色々処理書いてあるし、Administrateデフォルトのアクションとかをオーバーライドしたくなかったので、オーバーライドしないでorderを変更しました。
解決方法
Administrateで生成したcontrollers/admin/hoges_controller.rb
のorderを変更したいとする
class Admin::HogesController < Admin::ApplicationController
before_action :default_params
# To customize the behavior of this controller,
# simply overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Attachment.all.paginate(10, params[:page])
# end
# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Attachment.find_by!(slug: param)
# end
# See https://administrate-docs.herokuapp.com/customizing_controller_actions
# for more information
def default_params
params[:order] ||= 'updated_at'
params[:direction] ||= 'desc'
end
end
default_params
というアクションを作って、制御してあげればいい。