Search code examples
ruby-on-railspostgresqlpaperclipactiveadminglobalize

How to add translation for document


I have a table called 'documents' which has attachment (by paperclip gem), and I want to add translation for this attachment (by globalize gem) to be used in Activeadmin. So once I open the document page in the active admin, I would like to add two or more translations of the document but for the same model (same model id but only locales changes).

The schema create table DB table of Document model is:

create_table "documents", force: :cascade do |t|
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.string   "doc_file_name"
    t.string   "doc_content_type"
    t.integer  "doc_file_size"
    t.datetime "doc_updated_at"
    t.integer  "model_id"
  end

and the database is postgres.


Solution

  • Finally, I've solve it by removing the attachment 'doc' from the documents table then Creating translation table for documents using globalize gem Document.create_translation_table! and add:

    has_many :docs
     class Translation
        belongs_to :document
        has_attached_file :doc, MODEL_DOCUMENTS_STORAGE_OPTIONS
        validates_attachment_content_type :doc, content_type: ['application/pdf']
      end
    

    to the document model, then finally access it (create / update) by active admin form:

    form :html => { :enctype => 'multipart/form-data' } do |f|
        f.inputs 'Details' do
          f.translated_inputs 'ignored title', switch_locale: false do |t|
            t.input :doc, :as => :file
          end
        end
        actions
      end