LoginSignup
4
1

More than 5 years have passed since last update.

Rails管理画面用GemのAdministrateでorderを変更する

Posted at

はじめに

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というアクションを作って、制御してあげればいい。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1