ActiveAdmin Nested Form on #show

Can I add a sub form to the #show page?

Now I have my admin / posts.rb:

ActiveAdmin.register Post do
  show do |post|
    h2 post.title
    post.comments.each do |comment|
      row :comment do comment.text end
    end
  end
end

It lists all the comments for the post. Now I need a form to add new comments. I'm trying to do like this:

ActiveAdmin.register Post do
  show do |post|
    h2 post.title
    post.comments.each do |comment|
      row :comment do comment.text end
    end

    form do |f|
      f.has_many :comments do |c|
        c.input :text
      end
    end
  end
end

and get an error message:

undefined method has_many for <form> </form>: Arbr :: HTML :: Form

Models for posts and comments look like this:

class Post < ActiveRecord::Base
  has_many :comments
  accepts_nested_attributes_for :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

How to add this form to my display page? Thanks

+5
source share
2 answers

I use the following recipe when adding this type of information to the display page

    ActiveAdmin.register Post do
      show :title => :email do |post|
        attributes_table do
          row :id
          row :first_name
          row :last_name
        end
        div :class => "panel" do
          h3 "Comments"
          if post.comments and post.comments.count > 0
            div :class => "panel_contents" do
              div :class => "attributes_table" do
                table do
                  tr do
                    th do
                      "Comment Text"
                    end
                  end
                  tbody do
                    post.comments.each do |comment|
                      tr do
                        td do
                          comment.text
                        end
                      end
                    end
                  end
                end
              end
            end
          else
            h3 "No comments available"
          end
        end
      end
    end
+8
source

I did something similar for the has_one relationship:

ActiveAdmin.register Post do
  show :title => :email do |post|

    attributes_table do
      rows :id, :first_name, :last_name
    end

    panel 'Comments' do
      attributes_table_for post.comment do
        rows :text, :author, :date
      end
    end

  end
end

sorens, , .

+21

All Articles