show页面上的ActiveAdmin嵌套表单

show页面上的ActiveAdmin嵌套表单

本文介绍了#show页面上的ActiveAdmin嵌套表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在#show页面中添加嵌套表单?

Is it possible to add nested form to #show page?

现在我有我的admin / posts.rb:

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

并得到错误:

发布和评论模型如下:

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

推荐答案

在将此类信息添加到显示页面时,我使用以下食谱

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

这篇关于#show页面上的ActiveAdmin嵌套表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:21