0
votes

Im trying to upload multiple files with carrierwave and then upload them to cloudinary, but the problem is storing image urls into psql array column. I get this error:

ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR:  malformed array literal: "image/upload/v1460539908/u5fzgamsmt2rmnusees5.jpg"
LINE 1: UPDATE "products" SET "images" = 'image/upload/v1460539908/u...
                                     ^
DETAIL:  Array value must start with "{" or dimension information.
:UPDATE "products" SET "images" = 'image/upload/v1460539908/u5fzgamsmt2rmnusees5.jpg' WHERE "products"."id" = $1):
app/controllers/products_controller.rb:26:in `create'

Recieved params seems to be right, sending multiple files etc. SQL:

INSERT INTO "products" ("title", "images", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["title", ""], ["images", "{u5fzgamsmt2rmnusees5.jpg,oznphywjmfykswlzzgit.jpg,x4kzdzgggzpnf7ho0w5e.jpeg,v4dlu4sdtbbtiaspvmu2.jpeg}"], ["created_at", "2016-04-13 09:31:48.306261"], ["updated_at", "2016-04-13 09:31:48.306261"]]

It does start with "{}", or it wants it without literar ""?

Migration:

class AddImagesToProducts < ActiveRecord::Migration
  def change
    add_column :products, :images, :string, array: true, default: []
  end
end

Schema:

create_table "products", force: :cascade do |t|
t.string   "title"
t.text     "description"
t.decimal  "price"
t.datetime "created_at",                 null: false
t.datetime "updated_at",                 null: false
t.integer  "department_id"
t.integer  "category_id"
t.string   "images",        default: [],              array: true
end

Controller:

def create
  @product = Product.new(product_params)
  @product.save
end

private

  def product_params
    params.require(:product).permit(:title, {images: []})
  end

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

EDIT Changed params.require(:product).permit(:title, images: []) to params.require(:product).permit(:title, {images: []}) but still nothing

EDIT2 Form:

<%= stylesheet_link_tag "navbar" %>
<%= form_for @product, :html => {:multipart => true} do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.file_field :images, multiple: true %>
</p>
<p><%= f.submit %></p>
<% end %>
1

1 Answers

0
votes

So it looks like the problem is in cloudinary gem I was using, they have no support of psql json or array columns. So the way I got it working was in products controller, I added:

params[:product][:images].each do |image|
  Cloudinary::Uploader.upload(image)
end