简体   繁体   中英

rails_admin belongs_to field selection rename

I have the following model available in my app.

class Category < ActiveRecord::Base
  validates :name, :presence => true
  validates :name, :uniqueness => { :scope => :parent_id, :case_sensitive => false }

  belongs_to :parent, :class_name => "Category"
  has_many :children, :class_name => "Category", :foreign_key => "parent_id"
end

And I also have rails_admin enabled in my app. In the auto-generated admin site, I am having problem with selecting the parent and children category as shown in the image attached.

Rails Admin示例

In this example, I actually have "Desktop Memory" (child) under "Memory" (parent). How do I identify it in the dropdown menu and also the selection list? Perhaps with something like "Memory - Desktop Memory".

If you use some better tree implementation instead of the vanilla one, you can define the natural sort to be the tree sort, and the label to include hints for the nesting level.

For instance, in a nested_set implementation, you can use the :left field to sort, and the :depth field (if available) to prepend a number arrows to a custom method (eg :display_name ), obtaining for instance ">Desktop Memory" for the label of your child item.

Then just add your custom method as a preferred object_label_method to rails_admin:

RailsAdmin.config do |config|
  # ...
  config.label_methods.unshift :display_name
  # ...
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM