2012-07-10 39 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