Reputation: 122
I followed the tutorial on cloudinary.com to upload multiple (product) images to cloudinary. It works fine when I add a new product and upload a new image. When I try to edit/update the product, I want it to add the new image to an image array, but I get the error:
In my development.log:
Started PATCH "/admin/products/3" for 127.0.0.1 at 2018-05-04 09:35:12 +0200 Processing by Admin::ProductsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"4UcQNq49uJDpGAFyOxO3w07dhJFpIHkLYn2IqjLar+kWvNCC12c2Hjctq13rDOE476fPpm479fbV25XOr82bIQ==",$ ^[[1m^[[36mUser Load (0.9ms)^[[0m ^[[1m^[[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2^[[0$ ^[[1m^[[36mProduct Load (0.6ms)^[[0m ^[[1m^[[34mSELECT "products".* FROM "products" WHERE "products"."active" = $1 AND "products"."id" = $ ^[[1m^[[35m (0.3ms)^[[0m ^[[1m^[[35mBEGIN^[[0m ^[[1m^[[35mSQL (0.5ms)^[[0m ^[[1m^[[33mUPDATE "products" SET "images" = $1, "updated_at" = $2 WHERE "products"."id" = $3^[[0m [["images",$ ^[[1m^[[35m (2.9ms)^[[0m ^[[1m^[[35mCOMMIT^[[0m Completed 500 Internal Server Error in 21ms (ActiveRecord: 5.2ms)
NoMethodError (undefined method `reject' for "image/upload/v1525416518/t50yzb4cjtdnlyae4zx2.jpg":String):
app/controllers/admin/products_controller.rb:29:in `update'
Even if I don't upload a new image file, I still get the error. I think I did something wrong when setting up Cloudinary...
Here's my setup:
First I added the gems in my gemfile
gem 'carrierwave', github:'carrierwaveuploader/carrierwave'
gem 'cloudinary'
Then I created the cloudinary.yml file where I added my cloud_name, api_key and api_secret.
In my ProductsController I have set images as an array images: []:
def update
@product = Product.find(params[:id])
if @product.update_attributes(product_params)
redirect_to admin_products_path
else
redirect_to admin_products_path
end
end
private
def product_params
params.require(:product).permit(:name, :description, :price, :supply, :shipping_amount, :active, :product_number, images: [] )
end
end
In my views, the images file field looks like this:
<%= f.file_field :images, type: :file, multiple: true %><br />
Edit: Another thing I found out is that when I inspect the page, there's a messed up link:
What the link should be: http://res.cloudinary.com/dzidt5gxk/image/upload/v1525416534/exhgstwb354t4l6rqvmh.jpg
In the rails console it also shows that the images array is messed up:
<Product id: 3, name: "Ananas", images: "[\"[\\\"image/upload/v1525416518/t50yzb4cjtdnlyae4zx2...", product_number: "342342">]>
Upvotes: 0
Views: 557
Reputation: 5609
According to the Carrierwave documentation :
Also, make sure your upload controller permits the multiple file upload attribute, pointing to an empty array in a hash. For example:
params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})
Maybe you just need to change your permit params.
Upvotes: 0