Search code examples
ruby-on-railsrubyruby-on-rails-4ruby-on-rails-5activeadmin

Generate Active Admin Comment during a Action


How can I use Ruby/Rails code to generate an ActiveAdmin comment - such as during a controller action, or in an ActiveRecord lifecycle callback? (For example, when I change the status of a Order record from 'closed' to 'open'.)


Solution

  • Let say you have a User model, and you want to create a comment to a particular user.

    in model:

    after_update :add_comment
    
     def add_comment 
      ActiveAdmin::Comment.create(
        resource_id: User.last.id, # id of that particular user to which you add comment
        namespace: 'admin',
        body: 'Your comment body',
        resource_type: 'User',
        author_id: 1, # id of the comment's author, could be AdminUser.first, for example
        author_type: 'AdminUser'
      )
    end