2015-01-14 2 views
0

내 프로젝트에 ActiveAdmin gem을 사용하고 있습니다.액티브 관리자의 도서에 태그 추가

has_many 연관을 사용하는 2 개의 모델이 있습니다. 본질적으로 Book_Mapping Table을 통해 has_many 태그를 가진 Book 모델을 가지고 있습니다.

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 

답변

2

은 아마 당신은 내가 생각 CommunityResource하지 도서 모델을 등록하고 tag_ids: []을 permit_params에 추가하십시오한다. 양식 방법은 다음과 같습니다.

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 
+0

어떻게하면 내보기 페이지에서 선택된 태그를 표시 할 수 있습니까? 아니면 activeadmin을 통해 표시 할 수 있습니까? 위의 코드를 업데이트했습니다 ... –

+0

정말 고맙습니다. :) –

관련 문제