2012-07-10 52 views
5

Có thể thêm biểu mẫu lồng vào #show page không?Biểu mẫu lồng nhau ActiveAdmin trên #show trang

Bây giờ tôi có quản trị của tôi/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 

Nó liệt kê tất cả các bình luận cho bài. Bây giờ tôi cần một biểu mẫu để thêm nhận xét mới. Tôi đang cố gắng để làm như thế này:

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 

và nhận được một lỗi:

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

Models cho Bưu Comments trông giống như:

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

class Comment < ActiveRecord::Base 
    belongs_to :post 
end 

Làm thế nào để tôi thêm hình thức mà đến trang chương trình của tôi? Cảm ơn

Trả lời

8

tôi sử dụng công thức sau khi thêm loại thông tin để một chương trình trang

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

Làm cách nào để thêm văn bản nhập liệu cho nhận xét? – WarLord

21

tôi đã làm một cái gì đó như thế này cho một mối quan hệ 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 

Nếu bạn không cần sự linh hoạt bổ sung của giải pháp sorens tôi đặt cược bạn có thể làm việc với điều đó.

+0

Giải pháp này sạch hơn nhiều :) –

+0

Bất kỳ ý tưởng nào về cách thay đổi nhãn cho mục hàng? –

+1

bạn có thể xác định '' 'hàng''' một cách riêng biệt và chuyển vào một khối như sau:' '' row ("tiêu đề của bạn") {post.first_name} '' ' – Jim

Các vấn đề liên quan