1

我正在尝试在管理仪表板中设置操作文本。我已按照Rails Guides 概述页面上的安装步骤进行操作。然后,当尝试将属性添加到 /app/dashboards/lesson_dashboard.rb 时,出现错误。

当我尝试body: Field::RichText时,我得到:uninitialized constant Administrate::Field::RichText

当我尝试body: ActionText::RichText时,我得到:wrong number of arguments (given 4, expected 0..1)

当我按照Administrate Wiki 中的说明进行操作时,undefined method 'body'当它到达与<%= f.rich_text_area field.attribute %>.

知道这个属性叫什么或者为什么我会收到这些错误吗?

应用程序/仪表板/lesson_dashboard.rb:

require "administrate/base_dashboard"

class LessonDashboard < Administrate::BaseDashboard
  # ATTRIBUTE_TYPES
  # a hash that describes the type of each of the model's fields.
  #
  # Each different type represents an Administrate::Field object,
  # which determines how the attribute is displayed
  # on pages throughout the dashboard.
  ATTRIBUTE_TYPES = {
    course: Field::BelongsTo,
    documents: Field::ActiveStorage.with_options(
      show_display_preview: false,
      destroy_url: proc do |namespace, resource, document|
        [:delete_document_course, resource, { document_id: document.id }]
      end
    ),
    id: Field::Number,
    title: Field::String,
    # body: Field::Text,
    body: RichTextAreaField,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
  }.freeze

  # COLLECTION_ATTRIBUTES
  # an array of attributes that will be displayed on the model's index page.
  #
  # By default, it's limited to four items to reduce clutter on index pages.
  # Feel free to add, remove, or rearrange items.
  COLLECTION_ATTRIBUTES = %i[
    title
    course
  ].freeze

  # SHOW_PAGE_ATTRIBUTES
  # an array of attributes that will be displayed on the model's show page.
  SHOW_PAGE_ATTRIBUTES = %i[
    course
    title
    body
    documents
    created_at
    updated_at
  ].freeze

  # FORM_ATTRIBUTES
  # an array of attributes that will be displayed
  # on the model's form (`new` and `edit`) pages.
  FORM_ATTRIBUTES = %i[
    course
    title
    body
    documents
  ].freeze

  # COLLECTION_FILTERS
  # a hash that defines filters that can be used while searching via the search
  # field of the dashboard.
  #
  # For example to add an option to search for open resources by typing "open:"
  # in the search field:
  #
  #   COLLECTION_FILTERS = {
  #     open: ->(resources) { resources.where(open: true) }
  #   }.freeze
  COLLECTION_FILTERS = {}.freeze

  # Overwrite this method to customize how lessons are displayed
  # across all pages of the admin dashboard.
  def display_resource(lesson)
    lesson.title
  end

  # permitted for has_many_attached
  def permitted_attributes
    # super + [:attachments => []]
    super + [:documents => []]
  end

end

应用程序/模型/课程.rb:

class Lesson < ApplicationRecord

  belongs_to :course
  has_many_attached :documents

  validates :title, presence: true

end
4

1 回答 1

1

ActionText指南和 Administrate 帮助有这些信息,但它在多个页面中有些脱节,所以我记下了我在新的 Rails 6.1 应用程序中使用的步骤。

这些步骤假设您已经配置了 ActionText(运行rails g action_text:install并运行生成的迁移),并安装/配置了administrategem。

  1. Trix 富文本编辑器需要 Javascript,它在 Rails 6 中是通过 webpack 加载的。默认的管理布局不是 webpack 友好的,所以你需要自定义布局。生成要自定义的布局:
  % rails generate administrate:views:layout
  1. 添加javascript_pack_tagtoviews/layouts/admin/application.html.erb以加载您的应用程序包:
<head>
...
  <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  <%= render "stylesheet" %>
  <%= csrf_meta_tags %>
...
</head>

管理指南将引导您为管理 JS 创建一个单独的包,但仅当您需要自定义 JS(例如,flatpickr用作日期选择器)时才需要它。对于只使用 RichText 来说,它不是必需的,所以我们只加载主应用程序包。

  1. 您还需要自定义管理 CSS 以添加 Trix 样式。运行任务以生成自定义管理样式表:
% rails generate administrate:assets:stylesheets
  1. 编辑管理样式表以在底部包含 Trix 富文本控件的样式:
// Rest of file
...
@import "trix/dist/trix";
@import "selectize";

.trix-content {
  .attachment-gallery {
    > action-text-attachment,
    > .attachment {
      flex: 1 0 33%;
      padding: 0 0.5em;
      max-width: 33%;
    }

    &.attachment-gallery--2,
    &.attachment-gallery--4 {
      > action-text-attachment,
      > .attachment {
        flex-basis: 50%;
        max-width: 50%;
      }
    }
  }

  action-text-attachment {
    .attachment {
      padding: 0 !important;
      max-width: 100% !important;
    }
  }
}

.field-unit--rich-text-area-field {
  .field-unit__field {
    width: 80%;
  }
}
  1. 为富文本编辑添加自定义字段类型,因为 Administrate 不包括开箱即用的字段类型。
% rails g administrate:field rich_text_area
  1. 编辑输入字段类型fields/rich_text_area/_form.html.erb以使用 Trix 富文本控件:
<div class="field-unit__field">
  <%= f.rich_text_area field.attribute %>
</div>
  1. 编辑您的模型以添加您的富文本属性:
class Lesson < ApplicationRecord
  has_rich_text :body
end
  1. 为您的模型生成仪表板:
% rails generate administrate:dashboard Lesson
  1. 编辑字段类型dashboards/lesson_dashboard.rb以使用自定义字段类型:
  ATTRIBUTE_TYPES = {
    ...
    rich_text_body: RichTextAreaField,
  }.freeze
  1. 利润: 在此处输入图像描述
于 2021-08-20T22:00:24.040 回答