Uploading multiple files at once to Rails app with Carrierwave (HTML5)

后端 未结 2 346
情书的邮戳
情书的邮戳 2020-12-29 14:28

I\'m close...very close...I\'m able to upload single files just fine...but when i change the type of my form\'s file_field to :multiple => true

相关标签:
2条回答
  • 2020-12-29 14:50

    You can do a little hack for the params array, something like:

    aux = []
    params[:gallery][:photos_attributes][:image].each do |f|
      aux << {:image => f}
    end
    params[:post][:photos_attributes] = aux
    
    @gallery = Gallery.new(params[:gallery])
    

    I have a similar problem, I know it's an ugly hack, but it works for me.

    0 讨论(0)
  • 2020-12-29 15:06

    Ditch accepts_nested_attributes_for and instead add this to your Gallery model.

    def photos=(attrs)
      attrs.each { |attr| self.photos.build(:image => attr) }
    end
    

    Also, make sure that photos is in your gallery's accessible attribs in case you're protecting against mass assignment. Otherwise you won't get the photos array hash assignment from your params. I.e.

    attr_accessible :field1, field2, :photos
    
    0 讨论(0)
提交回复
热议问题