我使用我的rails app carrierwave gem
来上传多张图片(附件)。
附件正在项目模型中保存:
create_table "items", force: :cascade do |t|
t.string "title"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.json "attachments"
end
这个项目与允许的参数一起创建动作:
def items_create
@categories = Category.all
@item = Item.new(item_params)
@item.store_id = current_store.id
@item.account_id = current_store.account.id
respond_to do |format|
if @item.save
format.html { redirect_to store_items_show_path_path(current_store), notice: 'Item was successfully created.' }
format.json { render :template => "stores/items/show", status: :created, location: @item }
else
format.html { render :template => "stores/items/new" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
def item_params
params.require(:item).permit(:title, :description, attachments: [])
end
现在我尝试从here实施How to: Add more files and remove single file when using default multiple file uploads feature
。
这是我的设置:
def create_image
add_more_images(images_params[:attachments])
flash[:error] = "Failed uploading attachments" unless @item.save
redirect_back(fallback_location: root_path)
end
def destroy_image
remove_image_at_index(params[:id].to_i)
flash[:error] = "Failed deleting attachment" unless @item.save
redirect_back(fallback_location: root_path)
end
private
def add_more_images(new_images)
attachments = @item.attachments
attachments += new_images
@item.attachments = attachments
end
def remove_image_at_index(index)
remain_attachments = @item.attachments # copy the array
deleted_attachment = remain_attachments.delete_at(index) # delete the target attachment
deleted_attachment.try(:remove!) # delete attachment from S3
@item.attachments = remain_attachments # re-assign back
@item.remove_attachments! if remain_attachments.empty?
end
def images_params
params.require(:item).permit({attachments: []}) # allow nested params as array
end
的routes.rb
match 'stores/items/:item_id/attachments/:id'=> 'stores#destroy_image', :via => :delete, :as => :remove_attachment
post "store/items/:item_id/attachments/:id"=> "stores#create_image", :as => :create_image
我用它上传了一个文件:
<%= form_for @item, url: create_image_path(@item), method: :post do |form| %>
<div class="form-group">
<label class="btn btn-default">Keep old and add more images<span style="display:none;">
<%= form.file_field :attachments, multiple: true, id: "files" %></span></label>
</div>
<%= form.submit "Upload new images", :class=>"btn btn-default" %>
<% end %>
但是不能删除我循环的每个附件:
<h3>Currently uploaded images</h3>
<% @item.attachments.each_with_index do |attachments, index| #grab the index %>
<%= image_tag(attachments.url(:mini)) %>
<%= link_to 'Remove', remove_attachment_path(@item, index), data: { confirm: "Are you sure you want to delete this image?" }, :method => :delete %>
<% end %>
这是我每次点击删除时出现的错误:
ActiveRecord::RecordNotFound in StoresController#destroy_image
Couldn't find Item with 'id'=2
在此网址中:https://localhost:3000/stores/items/29/attachments/2
并在此行:
def set_item
@item = Item.find(params[:id])
end
有关如何修复删除单个附件功能的任何想法??
提前致谢!