Serhii Danovskyi Serhii Danovskyi - 2 months ago 14
Ruby Question

RailsAdmin label

how to change navigation label at rails_admin ?

I have model admin.rb

/app/models/admin.rb

module Admin
def self.table_name_prefix
'admin_'
end
end


and seo.rb

/app/models/admin/seo.rb

class Admin::Seo < ActiveRecord::Base

end


i'd like to change label to Admin model in Rails Admin

I edit /config/locales/ru.yml for admin/seo: it works fine but for Admin model doesn't work

activerecord:
models:
admin:
one: test
other: test1
admin/seo:
one: SEO - данные
other: SEO - данные


also i tried in /config/initializers/rails_admin.rb but it doesn't work

config.model Admin do
label 'test1'
label_plural 'test1'
navigation_label 'test1'
end


help me please

Answer

In my project it works like this

activerecord:
  models:
    admin/seo:
      one: SEO - данные
      other: SEO - данные

And you would not need your initializer.
Also it should be ru.yml not en.yml because it translation for Russian language so it must be used only for Russian locale.

UPDATE 1

I found some answer but I am not sure it is the best one (but it works)

class Admin::Seo < ActiveRecord::Base
  rails_admin do
    navigation_label I18n.t('your.translation.path.here')
  end
end

You can also put this code inside initializer not model (it is up to you).

You can see here how rails_admin generates this sidebar.
So each model should have navigation_label or it would be default t('admin.misc.navigation').

UPDATE 2

You can set navigation_label for all the models during initialization like this

# config/environment.rb
...

RailsAdmin::Config.models.each do |model|
  if model.abstract_model.model_name.starts_with? 'Admin::'
    model.navigation_label I18n.t('your.translation.path.here')
  end
end

This will split all models with Admin namespace and other models into two separate menus in sidebar.
The code is placed in environment.rb because translations are not available in initializers/*.rb.

Comments