fae icon indicating copy to clipboard operation
fae copied to clipboard

nested form with images in static pages

Open caplod opened this issue 9 years ago • 4 comments

I created a static page ‚gallery‘ . there I want to include a form where the user can upload multiple images.

# app/models/gallery_page.rb
class GalleryPage < Fae::StaticPage

 @slug = 'gallery'

 # required to set the has_one associations, Fae::StaticPage will build these associations dynamically
 def self.fae_fields
   {
     body: { type: Fae::TextArea }
   }
 end

 has_many :gallery_images, -> { where(attached_as: 'gallery_images') },
   as: :imageable,
   class_name: '::Fae::Image',
   dependent: :destroy
 accepts_nested_attributes_for :gallery_images, allow_destroy: true

end
# app/views/admin/content_blocks/gallery.html.slim
= simple_form_for @item, url: fae.update_content_block_path(slug: @item.slug), method: :put do |f|
 header.content-header.js-content-header
   == render 'fae/shared/form_header', header: @item

 main.content

   = fae_content_form f, :body, markdown: true

   / = fae_image_form f, :gallery_images <- this isnt working

how can I implement this?

caplod avatar Apr 13 '17 09:04 caplod

Yes please i also struggle with this, dont know how to do with the nested form for multiple images upload for my product model which is have many images

ferryphang avatar Apr 13 '17 09:04 ferryphang

Hi @caplod! You are able to add dynamic relationships to static pages, you'll just need to configure some things first.

First, add static_page_id to your gallery_items table. The association to gallery_items in gallery_page.rb will be simplified and move to the static_page_concern.rb that you'll need to override, then you can add the nested form to your page's form.

Please follow these steps outlined in the wiki, they should hopefully clear things up: https://github.com/wearefine/fae/blob/master/docs/topics/pages.md#configuring-a-dynamic-relationship-with-a-page-model

Hope that answers your question!

LeChin avatar Apr 13 '17 15:04 LeChin

One thing I just noticed, and will submit a PR for, is that you no longer need to add anything to your nested object's controller.

Instead of

module Admin
  class PromosController < Fae::ApplicationController
    def new
      @item = Promo.new
      @item.static_page_id = params[:item_id]
      build_assets
    end

 end
end

you simply need to add the following to your nested (gallery item) model.

  def fae_nested_parent
    :static_page
  end

LeChin avatar Apr 13 '17 15:04 LeChin

Great! I solved it using the steps in the wiki. and I also only used the fae_nested_parent method. Thanks for the hint

caplod avatar Apr 19 '17 07:04 caplod