在编辑期间添加活跃的管理员评论

Add active admin comments during edit

我正在尝试将 ActiveAdmin::Comment 添加到我的 Member 编辑中。我已经能够通过添加一个 ARB 和一个 partial

来做到这一点
#_comments.html.arb
active_admin_comments_for(resource)

这显示正常,但是当我输入文本然后按添加评论按钮时,实际上并没有添加评论,它只是返回到显示屏幕。

我想做的是在其中获取评论,但没有“添加评论”按钮。我想通过按 Update Member 按钮添加评论。这样,对成员所做的任何更改都将与评论同时保存。

有没有办法使用 Update Member 按钮添加评论?

编辑:

我也试过在我的模型中添加关系

#model
has_many :comments, as: :resource, dependent: :destroy, class_name: 'ActiveAdmin::Comment'
accepts_nested_attributes_for :comments, reject_if: :reject_comment


# members.rb - form
f.inputs "Add A Comment" do
  f.semantic_fields_for :comments, ActiveAdmin::Comment.new do |c|
    c.inputs :class => "" do
      c.input :resource_id, :input_html => { :value => "1" }, as: :hidden
      c.input :resource_type, :input_html => { :value => "Member" }, as: :hidden
      c.input :namespace, :input_html => { :value => :admin }, as: :hidden
      c.input :body, :label => "Comment"
    end
  end
end

然而,即使使用允许的参数,它仍然不会保存为评论。

我终于让这个工作起来了。

models/Members.rb

has_many :comments, as: :resource, dependent: :destroy, class_name: 'ActiveAdmin::Comment'
accepts_nested_attributes_for :comments, reject_if: :reject_comment

def reject_comment(comment)
    return comment['body'].blank?
end

app/Members.rb

# in the controller section of controller do
def update(options={}, &block)
  params[:member][:comments_attributes]['0']['namespace'] = 'admin'
  params[:member][:comments_attributes]['0']['author_id'] = current_admin_user.id
  params[:member][:comments_attributes]['0']['author_type'] = 'AdminUser'
  # This is taken from the active_admin code
  super do |success, failure|
    block.call(success, failure) if block
    failure.html { render :edit }
  end
end

# in the form portion
f.inputs "Add A Comment" do
  f.semantic_fields_for :comments, ActiveAdmin::Comment.new do |c|
    c.inputs :class => "" do
      c.input :body, :label => "Comment", :input_html => { :rows => 4 }
    end
  end
end

这允许我有一个名为 Comment 的字段,如果我想在编辑 Member 时添加评论,我可以这样做并在按下 [=14] 时保存评论=] 按钮 Update Member

所以最近我偶然发现了同样的问题,这是我的版本,它完全基于上面的 Bot ,但更通用一些:

型号

class Product < ActiveRecord::Base
  has_many :comments, as: :resource, dependent: :destroy,
           class_name: 'ActiveAdmin::Comment'

  accepts_nested_attributes_for :comments, allow_destroy: true,
                                reject_if: -> (comment) { comment['body'].blank? }
end

ActiveAdmin 资源

明确列出所有 comments_attributes' 属性是必要的。

ActiveAdmin.register Product do
  permit_params comments_attributes: [:author_id, :author_type, :body, :namespace, :_destroy]
end

ActiveAdmin 控制器中的补丁 update

ActiveAdmin.register Product do
  controller do
    def update(options={}, &block)

      # Adds known data of ActiveAdmin comment
      params[resource.class.name.downcase][:comments_attributes]['0'].merge!({
        author_id:   current_admin_user.id,
        author_type: current_admin_user.class.name,
        namespace:   ActiveAdmin::Devise.config[:path]
      })

      super do |success, failure|
        block.call(success, failure) if block
        failure.html { render :edit }
      end
    end
  end
end

请注意,这里我们动态获取基本资源 resource.class.name.downcase 的类型 class,管理员用户的 class 和 ID,以及管理员的设计范围(有些人自定义他们的/admin 命名空间):ActiveAdmin::Devise.config[:path]

表单域

ActiveAdmin.register Product do
  form do |f|
    f.inputs ActiveAdmin::Comment.model_name.human(count: "other") do
      # Remove `, ActiveAdmin::Comment.new`, and all the previous comments will be rendered as <textareax>
      f.semantic_fields_for :comments, ActiveAdmin::Comment.new do |c|
        c.input :body, input_html: { rows: 4, required: false }
      end
    end

    f.actions
  end
end

另类。这样所有现有评论将显示为 <textarea>s:

ActiveAdmin.register Product do
  form do |f|
    f.has_many :comments, allow_destroy: true do |c|
      c.input :body, input_html: { rows: 4, required: false }
    end

    f.actions
  end
end

如有需要请添加翻译

active_admin_comment.yml

---
ru:
  activerecord:
    models:
      active_admin/comment:
        zero:  Служебных комментариев
        one:   Служебный комментарий
        few:   Служебных комментария
        many:  Служебных комментариев
        other: Служебные комментарии

    attributes:
      active_admin/comment:
        body: Комментарий