ActiveAdmin在#show页面上嵌套的表单

时间:2012-07-10 06:51:51

标签: ruby-on-rails ruby-on-rails-3 activeadmin

是否可以将嵌套表单添加到#show页面?

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

列出了帖子的所有评论。 现在我需要一个表单来添加新评论。 我想这样做:

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

并收到错误:

  

未定义的方法`has_many'用于< form>< / form> :ARBRE :: HTML ::表格

帖子和评论的模型如下:

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

class Comment < ActiveRecord::Base
  belongs_to :post
end

如何将该表单添加到我的展示页面? 感谢

2 个答案:

答案 0 :(得分: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

如果你不需要增加灵活性的话,那就是#surens&#39;解决方案我打赌你可以解决这个问题。

答案 1 :(得分:10)

将此类信息添加到节目页面时,我使用以下方法

    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
相关问题