2

I've been struggling for a week now, I'm trying to create a form inside active_admin where a user can select several pictures, add a description and a title, then submitting his form to create something that would look like a gallery

So far, I have two models created with the commands:

rails g model Gallery title:string description:text
rails g model Image url:text #just in case the user has LOTS of images to upload

Here are how look my models now:

gallery.rb

class Gallery < ApplicationRecord
  has_many :images
  accepts_nested_attributes_for :images, allow_destroy: true
end

image.rb

class Image < ApplicationRecord

  belongs_to :gallery
  mount_uploader :image, ImageUploader #Using Carrier Wave
end

admin/gallery.rb

  permit_params :title, :description, :images

  form html: { multipart: true }  do |f|
    f.inputs  do
      f.input :title
      f.input :description
      f.input :images, as: :file, input_html: { multiple: true }
    end
    f.actions
  end

My problem is that even though my 'image' form shows up, I can't manage to save the images through the other model, nothing gets uploaded inside my 'public/upload' directory, and nothing gets written inside my database.

I can't find anything interesting internet that could solve this problem

Feel free to ask for another file

Any help welcome

Aamir
  • 16,329
  • 10
  • 59
  • 65
Jaeger
  • 1,686
  • 1
  • 30
  • 50
  • You can solve your problem by changing `permit_params` call (**admin/gallery.rb**): `permit_params :title, :description, images_attributes: [:image]` Hope, it helps – Andrei Nov 19 '16 at 20:16

1 Answers1

3

permit_params :title, :description, :images

Why :images, I think you meant images_attributes: [:url]?

But that wouldn't work either. I followed the steps here: https://github.com/carrierwaveuploader/carrierwave/issues/1653#issuecomment-121248254

You can do it with just one model

rails g model Gallery title:string description:text url:string

models/gallery.rb

# your url is accepted as an array, that way you can attach many urls
serialize :url, Array
mount_uploaders :url, ImageUploader

Note: Use serialize with Sqlite, For Postgres or some other database capable of handling arrays read: Add an array column in Rails

admin/gallery.rb

permit_params :title, :description, url: []
form html: { multipart: true }  do |f|
  f.inputs  do
    f.input :title
    f.input :description
    f.input :url, as: :file, input_html: { multiple: true }
  end
  f.actions
end
duacos
  • 69
  • 11