2012-07-10 50 views
5

是否可以将嵌套窗体添加到#显示页面?#显示页面上的ActiveAdmin嵌套窗体

现在我有我的管理/ 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 

它列出了所有职位的评论。 现在我需要一个表单来添加新评论。 我试着这样做:

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 

,并得到一个错误:

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

模型邮政和评论是这样的:

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

class Comment < ActiveRecord::Base 
    belongs_to :post 
end 

如何添加这种形式到我的展示页面? 感谢

回答

8

添加这类信息的一个节目页

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 
+0

我们如何为注释添加输入textarea? – WarLord 2015-12-05 10:47:29

21

我做这样的事情了HAS_ONE关系,当我使用下面的方法:

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'解决方案的灵活性我敢打赌,你可以用它来工作。

+0

这种解决方案更清洁:) – 2013-09-02 14:22:39

+0

任何想法如何更改行项目的标签? – 2014-03-30 14:56:56

+1

你可以单独定义'''rows''',并像这样传入一个块:''''''''''''''''''{post.first_name}'''' – Jim 2014-03-30 23:25:10