6

Is it possible to add nested form to #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 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:

undefined method `has_many' for <form></form> :Arbre::HTML::Form

Models for Post and Comments look like:

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

class Comment < ActiveRecord::Base
  belongs_to :post
end

How do i add that form to my show page? Thanks

2 Answers 2

22

I did something like this for a has_one relation :

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

If you didn't need the added flexibility of sorens' solution I bet you could work with that.

Sign up to request clarification or add additional context in comments.

3 Comments

This solutions is much cleaner :)
Any idea how to change the label for row item?
you can define the rows separately and pass in a block like this : row("your title") { post.first_name }
10

I use the following recipe when adding this type of info to a show 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

1 Comment

How do we add an input textarea for comments?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.