如何使用 Cloudinary direct 正确更新或销毁图像?
Posted
技术标签:
【中文标题】如何使用 Cloudinary direct 正确更新或销毁图像?【英文标题】:How to properly update or destroy an image using Cloudinary direct? 【发布时间】:2013-08-08 02:20:11 【问题描述】:所以我有 Cloudinary 通过载波直接上传图像并正确关联它们,但是一旦它们被创建,我似乎无法更新或销毁它们。
更新时,图像已上传并可以通过我的仪表板使用,但相关记录永远不会改变,我总是看到原始图像。
尝试销毁图像时,我收到错误消息:Couldn't find Image with ID=1 for Finish with ID=11
其中Finish
是父模型并且图像字段是嵌套的。此外,Image
模式是多态的。我不确定为什么当隐藏的输入为 20 时它认为图像 id 为“1”。
任何想法我做错了什么?
这是生成的表格(如果有任何其他有用的代码可以发布,请告诉我):
<form accept-charset="UTF-8" action="/finishes/11" class="edit_finish" enctype="multipart/form-data" id="edit_finish_11" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" value="✓" type="hidden">
<input name="_method" value="put" type="hidden">
<input name="authenticity_token" value="xxx=" type="hidden">
</div>
<fieldset id="finishes">
<legend>
Finish
</legend>
<div class="active">
<div class="field">
<label for="finish_title">Title</label>
<input id="finish_title" name="finish[title]" size="30" value="Eggshell White" type="text">
</div>
<div class="singular-addable" id="finish-image-92807">
<div class="addable-group images">
<div class="image-field-group">
<div class="field">
<label for="finish_image_attributes_0_asset">Image</label>
<input class="cloudinary-fileupload" data-cloudinary-field="finish[image_attributes][0][asset]" data-form-data="'timestamp':1375824622,'callback':'http://localhost:8080/cloudinary_cors.html','signature':'xxx','api_key':'xxx'" data-url="https://api.cloudinary.com/v1_1/hmnxgsjti/auto/upload" name="file" type="file">
</div>
<div class="image-box">
<img src="http://res.cloudinary.com/hmnxgsjti/image/upload/t_hint/v1375727159/owybb0xaxdlhgfyvwtwx.jpg">
</div>
<div class="remove-fields">
<input class="destroy" id="finish_image_attributes_0__destroy" name="finish[image_attributes][0][_destroy]" value="false" type="hidden"><a href="#" class="remove"><i class="icon-remove-circle icon-large" title="Remove"></i></a>
</div>
</div>
<input id="finish_image_attributes_0_id" name="finish[image_attributes][0][id]" value="20" type="hidden">
</div>
</div>
</div>
</fieldset>
<div class="actions">
<input name="commit" value="Save" type="submit">
</div>
</form>
编辑:请求更多代码
image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
def extension_white_list
%w(jpg jpeg gif png)
end
end
image.rb
class Image < ActiveRecord::Base
default_scope order('images.id ASC')
attr_accessible :asset,
:asset_cache
belongs_to :imageable, polymorphic: true
mount_uploader :asset, ImageUploader
end
finish.rb
class Finish < ActiveRecord::Base
default_scope order('finishes.title ASC')
attr_accessible :name,
:title,
## belongs_to ##
## has_many ##
:sku_ids,
:compilation_ids,
## nested attributes ##
:image_attributes
has_many :skus
has_many :compilations
has_many :image, as: :imageable, :dependent => :destroy
accepts_nested_attributes_for :image, reject_if: proc |attrs| attrs['asset'].blank? && attrs['asset_cache'].blank? , allow_destroy: true
validates_presence_of :image
validates_presence_of :title
before_save :create_name
def self.skus(finish_id = :id)
@skus = Sku.where(:finish_id => finish_id)
return @skus
end
private
def create_name
self.name = title.parameterize
end
end
finishes_controller.rb
class FinishesController < ApplicationController
# Ajax Routes
def skus
@skus = Finish.skus(params[:id])
render "skus/_list", locals: type: params[:type] , layout: false
end
# REST Routes
def index
@finishes = Finish.all
respond_to do |format|
format.html
format.json render json: @finishes
end
end
def show
@product_ids = Product.skus_by_finish(params[:id]).map|sku| sku.product_id
@products = Product.where(:id => @product_ids).order(:name)
render "layouts/templates/list"
end
def new
@finish = Finish.new
@finish.image.build
respond_to do |format|
format.html
format.json render json: @finish
end
end
def edit
@finish = Finish.find(params[:id])
end
def create
@finish = Finish.new(params[:finish])
respond_to do |format|
if @finish.save
format.html redirect_to finishes_path, notice: 'Finish was successfully created.'
format.json render json: finishes_url, status: :created, location: @finish
else
format.html render action: "new"
format.json render json: @finish.errors, status: :unprocessable_entity
end
end
end
def update
@finish = Finish.find(params[:id])
respond_to do |format|
if @finish.update_attributes(params[:finish])
format.html redirect_to finishes_url, notice: 'Finish was successfully updated.'
format.json head :no_content
else
format.html render action: "edit"
format.json render json: @finish.errors, status: :unprocessable_entity
end
end
end
def destroy
@finish = Finish.find(params[:id])
@finish.destroy
respond_to do |format|
format.html redirect_to finishes_url
format.json head :no_content
end
end
end
【问题讨论】:
【参考方案1】:结果证明这根本不是 Cloudinary 问题。
1) 图像没有更新,因为我的 images_controller
中没有 update
或 destroy
方法(doh!)
2) 我收到了Couldn't find Image with ID=1 for Finish with ID=11
错误,因为我的 JS 标记了要删除的隐藏文件配置错误并更改了所有隐藏字段的值。虽然我不知道为什么它以前可以工作,因为它没有改变。
【讨论】:
以上是关于如何使用 Cloudinary direct 正确更新或销毁图像?的主要内容,如果未能解决你的问题,请参考以下文章
如何实现 cloudinary 以动态显示图像作为背景?我看不到正确获取它们
在 Cloudinary 中使用 ngf-resize 上传图片
如何为 Netlify CMS 正确设置 cloudinary?
如何在 jquery 中向 cloudinary 文件上传()添加可选参数?