【问题标题】:Add active admin comments during edit在编辑期间添加活跃的管理员评论
【发布时间】:2016-10-12 02:28:23
【问题描述】:

我正在尝试将ActiveAdmin::Comment 添加到我的Member 编辑中。我已经能够通过添加 ARB 和部分来做到这一点

#_comments.html.arb
active_admin_comments_for(resource)

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

我想要做的是将 cmets 放在那里,但没有添加评论按钮。我想通过按Update Member 按钮添加评论。这样,对成员所做的任何更改都将与评论同时保存。

有没有办法通过Update Member 按钮添加 cmets?

编辑:

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

#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

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

【问题讨论】:

    标签: ruby-on-rails-4 activeadmin


    【解决方案1】:

    我终于得到了这个工作。

    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 时添加评论,我可以这样做并在按下f.actions 按钮Update Member 时保存评论

    【讨论】:

    • 嘿-我实际上正在尝试做类似的事情-将 cmets 用作几乎 git 提交。谢谢你 - 这将成为我使用的一部分,但我也希望重载删除/销毁,以便“你确定你想要”弹出窗口是一个文本输入,强制一个人添加评论或不删除;你试过类似的东西吗?
    • 如何允许 cmets 参数?我收到错误,因为 unpermitted_pa​​rameters :cmets_attributes.... Rails 版本是 4.2.6
    【解决方案2】:

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

    型号

    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 的类型、管理员用户的类和 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
    

    另类。这样所有现有的 cmets 都将显示为 &lt;textarea&gt;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: Комментарий
    

    【讨论】:

      猜你喜欢
      • 2012-11-15
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多