Thermatix
Thermatix

Reputation: 2929

ActiveAdmin Generic Form Partial

I have the following form for uploading PDFs:

  f.inputs 'PDF' do
    f.has_many :pdfs, allow_destroy: true do |pdf_f|
      pdf_f.semantic_errors
      pdf_f.input :name
      pdf_f.input :description
      pdf_f.input :enabled
      pdf_f.input :link, label: 'file', as: :file
    end
  end

And a way to display the file:

   li do
      div "name: #{pdf.name}"
      div "filename: #{pdf.link.path.split('/').last}"
      div raw "description: <br> #{pdf.description}"
      div "enabled: #{pdf.enabled ? "yes" : "no"}"
    end

Pdf is a polymorphic object thus can appear on several models.

How do I create a generic shard partial for each of them that can then be re-used across ActiveAdmin so I don't have to recreate them every time?

Bear in mind the second one I expect to iterate over it by passing it in as a collection:

Upvotes: 0

Views: 638

Answers (1)

Thermatix
Thermatix

Reputation: 2929

For the form answer is to place the form in a partial that resides in:

app/views/active_admin/base/_pdf_form.erb

and then render it like this:

  render partial: 'pdf_form', locals: {f: f}

For the display parital you should place the partial at:

app/views/active_admin/base/_pdf.erb

and then render it like this:

render partial: 'pdf', collection: event.pdfs

The partial name needs to be in quotes otherwise rails won't look for it active_admin/base.

Upvotes: 2

Related Questions