简体   繁体   English

如何在管理宝石中关联祖先宝石数据

[英]How do I relate Ancestry gem data in Administrate gem

I'm using the Administrate gem in my Rails 6 application.我在 Rails 6 应用程序中使用了管理 gem。 I have created a category model and I'm using the ancestry gem to organise the tree structure etc. I'm having trouble trying to integrate the ancestry gem into Administrate.我创建了一个类别模型,我正在使用祖先 gem 来组织树结构等。我在尝试将祖先 gem 集成到管理中时遇到了麻烦。 Mainly an issue where the parent_id for ancestry gem is null so and 2. trying to display the child name/relationships in Administrate.主要是祖先 gem 的 parent_id 为空的问题,因此 2. 尝试在管理中显示子名称/关系。

  1. The Administrate gem because of the relationship will not accept a null value when editing the form is "ancestry" is added to the dashboard form in administrate.管理 gem 由于关系在编辑表单时不会接受空值,将“祖先”添加到管理中的仪表板表单。
  2. Another problem I am trying to overcome is displaying the associated name of a child the id that ancestry uses.我试图克服的另一个问题是显示祖先使用的 id 的孩子的关联名称。 So ancestry using a string field which helps locate the parent using the id of the category row.所以祖先使用字符串字段,它有助于使用类别行的 id 来定位父级。 I have no idea how to manipulate the Administrate dashboard/controller to display the field name.我不知道如何操作管理仪表板/控制器来显示字段名称。

Any help would be so much appreciated.任何帮助将不胜感激。 I've googled this one for a bit and cannot seem to find anything related to the same problem.我在谷歌上搜索了一段时间,似乎找不到与同一问题相关的任何内容。

Is it worth using Ancestry and Administrate together?是否值得一起使用 Ancestry 和管理? Or should I just create my own category controller and views to allow Admin to modify the category data?或者我应该创建自己的类别控制器和视图以允许管理员修改类别数据? I'd prefer to try to keep all admin stuff in Administrate if possible.如果可能,我更愿意尝试将所有管理内容保留在管理中。

Thanks a mil.谢谢一百万。

Just adding my solution below in case anyone else comes across the question.只需在下面添加我的解决方案,以防其他人遇到问题。

To allow Administrate to accept a null value even though there might be presence required because of the ancestry gem use of the relationship in the model.允许管理接受空值,即使由于模型中关系的祖先 gem 使用而可能需要存在。 eg.例如。

class Category < ApplicationRecord
  has_ancestry
end

You can override the 'resource params' in the Admin controller in Administrate.您可以在管理中覆盖管理控制器中的“资源参数”。

# Override `resource_params` if you want to transform the submitted
# data before it's persisted. For example, the following would turn all
# empty values into nil values. It uses other APIs such as `resource_class`
# and `dashboard`:

 def resource_params
   params.require(resource_class.model_name.param_key).
     permit(dashboard.permitted_attributes).
     transform_values { |value| value == "" ? nil : value }
 end

Part 2.第2部分。

In the Dashboard category, I wanted to change the ID to a descriptive name.在仪表板类别中,我想将 ID 更改为描述性名称。 Ancestry gem uses a string field to show the ID of the Parent Category. Ancestry gem 使用字符串字段来显示父类别的 ID。

Dashboards > category_dashboard.rb file仪表板 > category_dashboard.rb 文件

  ATTRIBUTE_TYPES = {
    ancestry: Field::Select.with_options(collection: Category.all.map do |cat| [cat.name, cat.id] end),
    #ancestry: Field::Text,
  }.freeze

I have made the attriubte_type a select with options value instead of text field and mapped the name and id.我已将 attriubte_type 设为带有选项值而不是文本字段的选择,并映射了名称和 ID。

This allowed me to select the ID's of a parent category seeing their names instead of ID's.这允许我选择父类别的 ID,查看他们的名字而不是 ID。 However, I could not select a Null value and I required to leave this select area blank if I wanted Part 1 to do its thing and make a blank field = null.但是,我无法选择 Null 值,并且如果我希望第 1 部分执行其操作并使空白字段 = null,则我需要将此选择区域留空。 Making the category a parent category when added by an Admin user.由管理员用户添加时使类别成为父类别。

You can generate all the fields in Administrate using a generate command.您可以使用 generate 命令在管理中生成所有字段。 (I'll add this to the comments once I have a chance). (一旦有机会,我会将其添加到评论中)。

Once added I could edit the Select field forms.添加后,我可以编辑选择字段表单。 This allowed to me add :include_blank => 'Primary Category' to the select field form in adminstrate.这允许我将 :include_blank => 'Primary Category' 添加到 adminstrate 的选择字段表单中。

   <% if field.selectable_options.first&.is_a?(Array) %>
       <%= f.select(
         field.attribute,
         options_from_collection_for_select(
           field.selectable_options,
           :last,
           :first,
           field.data.presence,
         ),
       :include_blank => 'Primary Category'
       ) %>
  <% else %>
    <%= f.select(
      field.attribute,
      options_from_collection_for_select(
        field.selectable_options,
        :to_s,
        :to_s,
        field.data.presence,
      ),
    :include_blank => 'Primary Category'
    ) %>
  <% end %>

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

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