我想通过Ajax删除图像。 我正在使用Paperclip进行上传。
我了解到,为了删除图像,我所要做的就是将其设置为nil。
示例:
@meth.picture = nil # this works
错误消息:
Paperclip::AdapterRegistry::NoHandlerError (No handler found for "nil"):
app/controllers/meths_controller.rb:116:in `block in update'
app/controllers/meths_controller.rb:115:in `update'
代码:
$(function() {
$('#delete_image').on("click", function() {
$('#image > img').remove();
//console.log("??");
var url = document.URL.split("/");
var meth_id = url[4];
console.log(meth_id);
$.ajax({
url: '/Methoden/'+meth_id,
type: 'PATCH',
dataType: 'json',
data: {"meth": {"picture": "nil"}},
complete: function (jqXHR, textStatus) {
},
success: function (data, textStatus, jqXHR) {
console.log(" ?? profit");
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
});
});
在Зелёный的帮助下,我做了以下事情:
data: {"meth": "delete_image"}
然后在我的控制器更新方法中:
if params[:meth] == "delete_image"
@meth.picture = nil
@meth.save
end
答案 0 :(得分:1)
问题是您将字符串((No handler found for "nil")
)传递给方法@meth.picture
。
发送"nil"
"delete"
或"clear"
之类的内容,并检入控制器if params[:meth] == "delete"
执行@meth.picture = nil
否则执行其他操作。