0

I have two models, Album and Image. These have has_and_belongs_to associations with eachother. An album has many images, and an image can belong to many albums. Is it correct to specify HABTM then?

After I create an album, it redirects to the album show path. On this page, I would like to allow the user to add (upload) images to the album. However, I'm not sure how to specify the form for this. I'm using simpleform. I tried something like this (haml):

  = simple_form_for [:admin, @album] do |a|
    = a.simple_fields_for :images do |i| 
      = i.input :title
      = i.file_field :image
      = i.submit

But that just updates the album and doesn't create the image. I also got an error saying "Image expected, got Array".

I would like to allow my users to upload images directly on the album show page. It feels wrong to have a separate image upload page where they specify the album. Makes a huge difference in UX, in my mind.

Or.. I guess I could have a link saying 'add image' and when clicked, render a partial, say image/_upload_form in a modal? That's a backup plan however. Looking for a solution for the image form on the album show page. Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
Joris Ooms
  • 11,880
  • 17
  • 67
  • 124

1 Answers1

2

You should submit the form,not the fields.

This code

= simple_form_for [:admin, @album] do |a|
    = a.simple_fields_for :images do |i| 
      = i.input :title
      = i.file_field :image
      = i.submit

should be

= simple_form_for [:admin, @album] do |a|
    = a.simple_fields_for :images do |i| 
      = i.input :title
      = i.file_field :image
    = a.submit #Here
Mandeep
  • 9,093
  • 2
  • 26
  • 36
Pavan
  • 33,316
  • 7
  • 50
  • 76