【问题标题】:Rails carrierwave multiple uploads psql array value must start with "{"Railscarrierwave 多次上传 psql 数组值必须以“{”开头
【发布时间】:2016-08-04 07:54:00
【问题描述】:

我尝试使用carrierwave 上传多个文件,然后将它们上传到cloudinary,但问题是将图像url 存储到psql 数组列中。 我收到此错误:

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'

收到的参数似乎是正确的,发送多个文件等。 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"]]

它确实以“{}”开头,还是希望它没有文字“”?

迁移:

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

架构:

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

控制器:

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

编辑 将 params.require(:product).permit(:title, images: []) 更改为 params.require(:product).permit(:title, {images: []}) 但仍然没有

EDIT2 表格:

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

【问题讨论】:

    标签: ruby-on-rails carrierwave psql


    【解决方案1】:

    所以看起来问题出在我使用的 cloudinary gem 中,它们不支持 psql json 或数组列。所以我让它工作的方式是在产品控制器中,我补充说:

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

    【讨论】:

      猜你喜欢
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 2015-02-15
      相关资源
      最近更新 更多