Adding Dynamic Relationships to Static Pages

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
module Fae
  module StaticPageConcern
    extend ActiveSupport::Concern

    included do
      has_many :promos, foreign_key: 'static_page_id'
    end

  end
end
class Promo < ActiveRecord::Base

  belongs_to :static_page, class_name: 'Fae::StaticPage', optional: true

end

module Admin
  class PromosController < Fae::NestedBaseController
  end
end
def fae_nested_parent
  :static_page
end
 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
    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