2016-04-13 4 views
0

Im versuchen, mehrere Dateien mit Carrierwave hochladen und laden Sie sie dann zu Cloudinary, aber das Problem speichert Bild URLs in psql Array-Spalte. ich diesen Fehler:Rails carrierwave mehrere Uploads psql Array-Wert muss mit "{"

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 scheint richtig zu sein, mehrere Dateien zu senden usw. 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"]] 

Es startet mit "{}", oder er will es ohne 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 Geänderte params.require (: Produkt) .permit (Titel, Bilder: [ ]) to params.require (: product) .permit (: title, {images: []}) aber immer noch nichts

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 %> 

Antwort

0

So sieht es aus wie das Problem in cloudinary Juwel war ich mit, sie keine Unterstützung von psql json oder Array-Spalten haben. So, wie ich es funktionierte, war in Produkt-Controller, fügte ich hinzu:

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