0

I'm having a problem uploading multiple images via Carrierwave and am not sure if it's a bug or user error (probably the latter). I'm doing everything in a rather standard way though (as per documentation) so it's weird that this doesn't work.

I have the following in my Gemfile: gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'

my image_uploader.rb:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process :resize_to_limit => [590, 590]
  end
  version :featured do
    process :resize_to_fill => [390, 390]
  end
  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

My article.rb file:

class Article < ActiveRecord::Base
  mount_uploaders :images, ImageUploader
end

I am letting the params pass from my controller with:

def article_params
  params.require(:article).permit(:title, :images, :body)
end

And the _form.html.erb partial uses:

<%= form_for @article, html: { multipart: true } do |f| %>
  <%= f.label :images %><br>
  <%= f.file_field :images, multiple: true %><br>
  <%= f.submit 'Update Article' %>
<% end %>

Oddly, when I upload two images, I am not seeing them passed into the article_params from the update method.

If I pry it, I can see that:

params.require(:article).permit(:images)
Unpermitted parameters: title, images, body
=> {}

Images seems to be unpermitted, even though I explicitly permit it...

Any clue as to what may be incorrect here?

DaniG2k
  • 4,772
  • 36
  • 77
  • Seems like you implemented multiple upload incorrectly. Look at [this post](http://stackoverflow.com/questions/21411988/rails-4-multiple-image-or-file-upload-using-carrierwave). It explain how to implement multiple upload with Carrierwave. – Maxim Mar 15 '15 at 13:18
  • @maxd I was working off the example on the official documentation: https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads I wonder if that's not the correct way to do it...? – DaniG2k Mar 15 '15 at 13:19

1 Answers1

0

The solution seems to be:

params.require(:article).permit(:title, :body, images: [])

If you take a look in pry on images param, its type is an Array:

> params[:article][:images].class
=> Array

From docs:

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

params.permit(id: [])

Source: 4.5.1 Permitted Scalar Values

Community
  • 1
  • 1
raphqu
  • 36
  • 2
  • Genius! Thanks so much for that. There's only one mistake: it should be `params.require(:article).permit(:title, :body, {images: []})` Images apparently needs to be wrapped around a hash. – DaniG2k Mar 15 '15 at 17:22
  • @raqhqu any clue how I'd remove individual uploads via a checkbox? Using something like `<%= f.check_box :remove_images %>` will remove all the uploaded files. – DaniG2k Mar 16 '15 at 13:09