Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add active admin comments during edit

I'm trying to add ActiveAdmin::Comment to my Member edit. I have been able to do this by adding an ARB and a partial

#_comments.html.arb
active_admin_comments_for(resource)

This displays properly but when I type in text then press the add comment button, the comment isn't actually added, it just goes back to the show screen.

What I am trying to do is get the comments in there, but not have the Add Comment button. I would like to add a comment by pressing the Update Member button. This way any changes done to the member will be saved with the comment at the same time.

Is there a way to have it so comments are added with the Update Member button?

EDIT:

I have also tried adding a relation in my model

#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

However, even with the permitted params it still doesn't save as a comment.

like image 441
Bot Avatar asked Oct 12 '16 02:10

Bot


1 Answers

I ended up finally getting this working.

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

This allows me to have a field called Comment and if I want to add the comment while I edit the Member I can do this and save the comment when I press the f.actions button Update Member

like image 106
Bot Avatar answered Jan 03 '23 16:01

Bot