0
我是ActiveAdmin和Rails的新手,我努力建立ActiveAdmin界面。ActiveAdmin與has_many形成多個嵌套的HMT關係
考慮以下型號:
class PageType < ActiveRecord::Base
has_many :fields, class_name: 'PageField'
accepts_nested_attributes_for :fields, allow_destroy: true
end
class PageField < ActiveRecord::Base
belongs_to :page_type
has_many :page_has_fields
has_many :pages, through: :page_has_fields
accepts_nested_attributes_for :page_has_fields, allow_destroy: true
end
class PageHasField < ActiveRecord::Base
belongs_to :page
belongs_to :page_field
end
class Page < ActiveRecord::Base
belongs_to :page_type
has_many :page_has_fields, dependent: :delete_all
has_many :page_fields, through: :page_has_fields
accepts_nested_attributes_for :page_fields, allow_destroy: true
end
在主動聯繫我想創造一些頁面模板來處理「靜態」的網頁。並且在每個頁面中,我想更新與模板頁面相關的每個字段的內容。
到目前爲止,我沒有這個代碼工作:
ActiveAdmin.register Page do
permit_params :name, :page_type_id, :page_id,
:page_fields_attributes => [:id, :name, :field_type, :page_id,
:page_has_fields_attributes => [:id, :content, :page_id]
]
form do |f|
f.inputs
f.has_many :page_fields, heading: false, new_record: false do |g|
g.inputs :name, :required
g.has_many :page_has_fields, new_record: false do |h|
h.input :content if h.object.page_id == f.object.id
end
end
f.actions
end
end
但第二次的has_many似乎真的我錯了,我敢肯定有一個更好的解決這個問題。 如果我不去與「如果」,輸入創建爲正確的領域,但爲每一個頁面。
有沒有辦法在has_many中指定ID或參數?或者更好的標籤來處理這種情況?
感謝
你把我放在正確的軌道,我用了一個雙「f.inputs爲:」因爲我想text_areas只有屬於這個特定頁面的字段和每個標籤。 再次感謝您! – LinaeAzn