简体   繁体   English

rails_admin所属字段选择重命名

[英]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. 我的应用程序中也启用了rails_admin。 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. 例如,在nested_set实现中,您可以使用:left字段进行排序,并使用:depth字段(如果可用)在自定义方法(例如:display_name )前添加数字箭头,以获取例如">Desktop Memory"为您的子项的标签。

Then just add your custom method as a preferred object_label_method to rails_admin: 然后只需将您的自定义方法作为首选的object_label_method添加到rails_admin中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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