Rails Carrierwave添加更多文件未定义的方法“标识符”

时间:2018-10-18 08:22:17

标签: ruby-on-rails carrierwave

我正在尝试实现添加更多文件并使用carrierwave gem删除单个文件。我遵循了发现here的说明。但是,当我尝试添加更多文件时,旧文件会变成NUL并由于某种原因而被删除。没有错误出现,但是如果我看一下控制台,我会得到:

SQL (0.5ms)  UPDATE "items" SET "images" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["images", "{NULL,NULL,image5.jpg,image6.jpg}"], ["updated_at", "2018-10-18 07:58:52.685554"], ["id", 85]] (0.4ms)  COMMIT

此外,当我尝试删除文件时,没有任何反应。没有错误出现,并且文件仍然保持原样,但是如果我看一下控制台,就会发现:

 SQL (0.6ms)  UPDATE "items" SET "images" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["images", "{NULL,NULL,image5.jpg,image6.jpg}"], ["updated_at", "2018-10-18 08:00:29.641571"], ["id", 85]] (0.4ms)  COMMIT

我不知道为什么会这样,并且我已经尝试解决了一段时间,因此,非常感谢您进行此工作。

这是我的设置:

我将此列添加到了项目模型中:

add_column :items, :images, :string, array: true, default: []

这些是我的路线:

match 'store/item/:id'=> 'attachments#destroy', :via => :delete, :as => :remove_item_image
post "store/item/:id"=> "attachments#create", :as => :create_item_image

控制器:

class AttachmentsController < ApplicationController
before_action :set_item

def create
  add_more_images(images_params[:images])
  flash[:error] = "Failed uploading images" unless @item.save
  redirect_back fallback_location: root_path
end

def destroy
  remove_image_at_index(params[:id].to_i)
  flash[:error] = "Failed deleting image" unless @item.save
  redirect_back fallback_location: root_path
end

private

def set_item
  @item = Item.find(params[:id])
end

def add_more_images(new_images)
  images = @item.images
  images += new_images
  @item.images = images
end

def remove_image_at_index(index)
  remain_images = @item.images # copy the array
  deleted_image = remain_images.delete_at(index) # delete the target image
  deleted_image.try(:remove!) # delete image from S3
  @item.images = remain_images # re-assign back
end

def images_params
  params.require(:item).permit({images: []}) # allow nested params as array
end
end

这是我浏览图像并添加删除链接的视图:

<% @item.images.each_with_index do |img, index| #grab the index %>
<%= image_tag(img.url(:mini)) %>
<%= link_to "Remove", remove_item_image_path(@item, index: index), data: { confirm: "Are you sure you want to delete this image?" }, :method => :delete %>
 <% end %>

这是添加更多图像的形式:

<%= form_for @item, url: create_item_image_path(@item), method: :post , :html => {:id => "form", :multipart => true } do |f| %>
 <%= f.file_field :images, multiple: true %>
 <%= f.submit 'Add more files' %>
 <% end %>

更新1

当我尝试从rails console手动添加本地图像时,我会这样做:

@item = Item.find(85)
@item.images << [File.open("#{Rails.root}/app/assets/images/no-image.jpg", 'rb')]

新的本地映像正在添加到数组中,但是当我执行此操作@item.save时,出现以下错误:

NoMethodError: undefined method `identifier' for #<Array:0x007fd2536ccd98>
    from (irb):4

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您正在向@ item.images关联中添加一个数组,它需要一个File并尝试在其上调用identifier。仅设置文件:

@item.images << File.open("#{Rails.root}/app/assets/images/no-image.jpg", 'rb')

如果您想一次添加多张图片,可以循环执行,或者@image.images对象有某种添加多张图片的方法(我在文档中找不到它,但是我想有一)。在您提供的链接上可以做到:

images += new_images