我的问题在于以编辑嵌套的形式显示图像。怎么办呢?
我有2个型号:产品和照片。
model photo.rb
belongs_to :photoable, polymorphic: true
model product.rb
has_many :photos, as: :photoable, :dependent => :destroy
accepts_nested_attributes_for :photos, allow_destroy: true
products_controller:
def edit
@product = Product.find(params[:id])
end
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to products_url, notice: 'Cập nhật thành công.!.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def product_params
params.require(:product).permit(:name, :description, :price, :category_id, photos_attributes: [:image, :photoable_id, :photoable_type, :position])
end
upload.js:
autoProcessQueue : false,
uploadMultiple: true,
parallelUploads: 5,
maxFilesize: 1,
acceptedMimeTypes: "image/*",
previewsContainer: "#previews",
paramName: "product[photos_attributes][][image]",
addRemoveLinks: true,
dictRemoveFile: "delete",
init: function() {
var myDropzone = this;
this.element.querySelector("#submit-all").addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
this.on("successmultiple", function(files, response) {
window.location.replace("http://localhost:3000/products");
});
},