Я пытаюсь увеличить новые значения в массиве, но старые значения удаляются.Как вы можете видеть ниже, у меня было одно изображение там, и теперь оно NULL
, но новое изображение там.
SQL (1.5ms) UPDATE "attachments" SET "media_files" = $1, "updated_at" = $2 WHERE "attachments"."id" = $3 [["media_files", "{NULL,image4.jpg}"], ["updated_at", "2018-10-25 09:12:05.564281"], ["id", 11]]
Я использую carrierwave gem
, и это метод, который яиметь внутри контроллера, чтобы сохранить существующие значения и увеличивать новые:
def create
files = @attachment.media_files # copy the old images
files += params[:item][:media_files] # add new file to the files
@attachment.assign_attributes(:media_files => files) # assign back
if @attachment.save
flash[:notice] = "Media files where successfully uploaded"
redirect_back fallback_location: root_path
else
flash[:alert] = "Failed to upload media files"
redirect_back fallback_location: root_path
end
end
И форма:
<%= form_for @item, url: create_image_path(@attachment), method: :post , :html => {:id => "form"} do |f| %>
<%= f.file_field :media_files, multiple: true %>
<%= f.submit 'Add' %>
<% end %>
Ассоциации моделей:
class Item < ApplicationRecord
has_many :attachments, dependent: :destroy
accepts_nested_attributes_for :attachments, allow_destroy: true
end
class Attachment < ApplicationRecord
belongs_to :item
mount_uploaders :media_files, AttachmentUploader
validates_presence_of :media_files
end
Схема для двух моделей:
create_table "attachments", force: :cascade do |t|
t.integer "item_id"
t.integer "account_id"
t.string "media_files", default: [], array: true
t.string "content_type"
t.boolean "success"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "items", force: :cascade do |t|
t.string "title"
t.string "description"
t.integer "category_id"
t.integer "account_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Маршрут для метода создания:
post "item/:id/uploads/media_files/:id"=> "attachments#create", :as => :create_image
Есть идеи, чего мне здесь не хватает?