u16suzuの blog

日々学んだことのメモブログです。

Rails Admin でナビゲーションに表示されるモデルの順番を指定する

Change models order in navigation

By default, they are ordered by alphabetical order. If you need to override this, specify a weight attribute. Default is 0. Lower values will bubble items to the top, higher values will move them to the bottom. Items with same weight will still be ordered by alphabetical order. The mechanism is fully compatible with navigation labels. Items will be ordered within their own menu subset. (but parent will always be first inside his submenu).

config/initializer/rails_admin.rb で、各モデルの表示順のウェイトを指定できる. 小さいほうが上、大きいほうが下. デフォルトはアルファベット順である

こんなかんじ↓

config.model 'League' do
  weight -1
end

さらにこんなかんじ↓で書くと、 カテゴリ名 'League related' の下に League 項目を表示できる。 つまりは階層構造みたいなものがつかえる。

config.model 'League' do
  navigation_label 'League related'
  weight -1
end

この書き方だと、1個ずつモデルとウェイトの組を書かなければいけない。 もっと良いruby の書き方がありそう。

参考: https://github.com/sferik/rails_admin/wiki/Navigation