Here is an example of a pattern you can use to associate objects to your Page models, i.e. for use in a nested form for an item like promos which will exist across many Page objects.
A few things are needed for this to work correctly:
add_column :promos, :static_page_id, :integer
static_page_concern.rb
to the model/concerns/fae
folder, add a relationship for promos:module Fae
module StaticPageConcern
extend ActiveSupport::Concern
included do
has_many :promos, foreign_key: 'static_page_id'
end
end
end
:static_page
, with the class_name
for the Page object.class Promo < ActiveRecord::Base
belongs_to :static_page, class_name: 'Fae::StaticPage', optional: true
end
Fae::NestedBaseController
instead of Fae::BaseController
module Admin
class PromosController < Fae::NestedBaseController
end
end
:static_page
.def fae_nested_parent
:static_page
end
parent_item
argument item virtual (which is just the instance of the AboutUsPage
, which we don’t have a column in the database for), you need to make the argument related to static pages more broadly. section.content
= render 'fae/shared/nested_table',
assoc: :promos,
parent_item: Fae::StaticPage.find_by_id(@item.id),
cols: [:headline, :body, :link],
ordered: true
.nested-form
for a more inline look. .nested-form
h2 New Promo
== render 'form
Lastly, in the object form be sure to add the static_page_id
as a hidden field in the promo objects form.
= simple_form_for([:admin, @item], html: {multipart: true, novalidate: true, class: 'js-file-form'}, remote: true, data: {type: "html"}) do |f|
= f.hidden_field :static_page_id
= f.submit