在Active Admin中为书籍添加标签

时间:2015-01-14 09:46:10

标签: ruby-on-rails ruby mapping activeadmin has-many-through

我在我的项目中使用ActiveAdmin gem。

我有2个模型使用has_many通过关联。基本上,我有一个Book模型,通过Book_Mapping表具有多个标签。

我希望能够在Active Admin中从我的图书表单编辑/添加标签。

但我无法在表格中显示此内容。任何人都可以帮我创建正确的Active Admin表单结构吗?

模型

class Book < ActiveRecord::Base

  has_many :book_mappings
  has_many :tags, through: :book_mappings

  ##Not sure if I should use this...
  accepts_nested_attributes_for :book_mappings
  accepts_nested_attributes_for :tags

end


class BookMapping < ActiveRecord::Base
  belongs_to :book
  belongs_to :tag
end

class Tag < ActiveRecord::Base

  has_many :book_mappings
  has_many :books, through: :book_mappings

end

ACTIVEADMIN

ActiveAdmin.register Book do

###Should this permit any other params? 
permit_params :title

form do |f|
  f.inputs "Book Detail" do
    f.input :title
  end

  f.has_many :book_mappings do |app_f|
    app_f.inputs "Book Tags" do

      ###Other than a Create Tag button, 
      ###The actual form fields don't appear at all...
      app_f.input :book_tag_id
    end
  end
end

#Show Page (Is there a way to show the selected tags here?)
  show do |pic|
    attributes_table do
      row :title

    end
  end

end

1 个答案:

答案 0 :(得分:2)

我认为您应该注册Book model而不是CommunityResource,请添加到permit_params tag_ids: []。表单方法可能如下所示:

form do |f|
  f.inputs "Book Detail" do
    f.input :title
  end

  f.inputs "Tags" do
    f.input :tags, as: :check_boxes
  end

  f.actions
end

如果您想在节目页面上显示标签,可以在侧栏中进行。

sidebar 'Tags', only: :show, if: proc { book.tags.any? } do
  table_for book.tags do |t|
    t.column('Name') { |tag| tag.name } # it's depends what you want to display  
  end
end