ActiveAdmin nested form on #show page

半城伤御伤魂 提交于 2019-12-20 14:42:14

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/11408060/activeadmin-nested-form-on-show-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!