【问题标题】:Cloudinary how to update/edit images in arrayCloudinary 如何更新/编辑数组中的图像
【发布时间】:2018-10-14 16:05:42
【问题描述】:

我按照 cloudinary.com 上的教程将多个(产品)图像上传到 cloudinary。当我添加新产品并上传新图片时,它工作正常。当我尝试编辑/更新产品时,我希望它将新图像添加到图像数组中,但出现错误:

在我的 development.log 中:

在 2018 年 5 月 4 日 09:35:12 为 127.0.0.1 启动 PATCH "/admin/products/3" +0200 Admin::ProductsController#update 处理为 HTML 参数:{"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^[[36m产品加载(0.6ms)^[[0m ^[[1m^[[34mSELECT "products".* FROM "products" WHERE "products"."active" = $1 AND "产品"."id" = $ ^[[1m^[[35m (0.3ms)^[[0m ^[[1m^[[35mBEGIN^[[0m ^[[1m^[[35mSQL (0.5ms)^[[0m ^[[1m^[[33mUPDATE“产品”SET“图像” = $1, "updated_at" = $2 WHERE "products"."id" = $3^[[0m [["images",$ ^[[1m^[[35m (2.9ms)^[[0m ^[[1m^ [[35mCOMMIT^[[0m 完成 500 21ms 内的内部服务器错误 (ActiveRecord: 5.2ms)

NoMethodError(未定义的方法“拒绝” “图片/上传/v1525416518/t50yzb4cjtdnlyae4zx2.jpg”:字符串):

app/controllers/admin/products_controller.rb:29:in `update'

即使我不上传新的图像文件,我仍然会收到错误消息。我认为我在设置 Cloudinary 时做错了...


这是我的设置:

首先我在我的 gemfile 中添加了 gems

gem 'carrierwave', github:'carrierwaveuploader/carrierwave'
gem 'cloudinary'

然后我创建了 cloudinary.yml 文件,在其中添加了我的 cloud_name、api_key 和 api_secret。

在我的 ProductsController 中,我将图像设置为数组图像:[]:

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

在我看来,图像文件字段如下所示:

<%= f.file_field :images, type: :file, multiple: true %><br />

编辑:我发现的另一件事是,当我检查页面时,有一个乱七八糟的链接:

http://res.cloudinary.com/dzidt5gxk/raw/upload/v1/%5B%22%5B%5C%22image/upload/v1525416518/t50yzb4cjtdnlyae4zx2.jpg_____

链接应该是什么: http://res.cloudinary.com/dzidt5gxk/image/upload/v1525416534/exhgstwb354t4l6rqvmh.jpg

在 Rails 控制台中,它还显示图像数组被弄乱了:

<Product id: 3, name: "Ananas", images: "[\"[\\\"image/upload/v1525416518/t50yzb4cjtdnlyae4zx2...", product_number: "342342">]>

【问题讨论】:

  • 请以纯文本形式发布日志,而不是图片。
  • 感谢反馈,我改了。

标签: ruby-on-rails ruby carrierwave cloudinary


【解决方案1】:

根据Carrierwave documentation

另外,请确保您的上传控制器允许多个文件 上传属性,指向散列中的空数组。例如:

params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})

也许您只需要更改您的许可参数。

【讨论】:

  • 我改变了 {images: []} 但它没有做任何事情。我仍然收到错误消息。
  • @Jakeroo 实际上这是cloudinary_gem (here) 中的一个已知和当前问题
猜你喜欢
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2013-08-08
  • 2020-09-04
  • 2021-12-15
  • 2012-01-31
  • 2018-02-19
相关资源
最近更新 更多