我正在尝试将ActiveAdmin::Comment
添加到我的Member
修改中。我已经能够通过添加ARB和部分
#_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
然而,即使使用允许的参数,它仍然不能保存为评论。
答案 0 :(得分:3)
我最终得到了这个工作。
<强>模型/ 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
应用/ 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
答案 1 :(得分:0)
最近我偶然发现了同样的问题,这是我的版本,它完全基于上面的 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
必须明确列出所有 comments_attributes
' 属性。
ActiveAdmin.register Product do
permit_params comments_attributes: [:author_id, :author_type, :body, :namespace, :_destroy]
end
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
的 tye 类,admin 用户的类和 id,以及 admin 的设计范围(有些人自定义了他们的 /admin
命名空间):{{ 1}}
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
s:
<textarea>
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