【发布时间】:2014-10-29 14:52:45
【问题描述】:
在我的 rails4 应用程序中,我有一个模型 Product,其中包含许多 ProductImages。我正在尝试通过使用 file_field 来上传 jquery 文件以在父表单中创建图像,并且我将我的字段设置为在选择文件时自动上传。我的 Product_images 控制器中只有创建和销毁方法。
但是,当它尝试发布到 URL:“/products/1/product_images”时,出现路由错误。
No route matches [PATCH] "/products/1/product_images"
我在浏览器控制台中看到我的请求方法是“POST”,但 rails 应用程序正在尝试将其匹配为 PATCH...
Remote Address:127.0.0.1:80
Request URL:http://appname.dev/products/1/product_images
Request Method:POST
Status Code:404 Not Found
是什么导致应用使用 PATCH?
Routes.rb
resources :products do
resources :product_images, only: [:create, :destroy]
end
图像的部分表单
<fieldset id="product-images">
<%= file_field_tag "product_images[]", type: :file, id:"file-select", multiple:true %>
<% f.fields_for :product_images do |builder| %>
<%= render "product_images/product_image", f:builder %>
<% end %>
</fieldset>
我的 Javascript
$(function(){
/* ---IMAGE AREA--- BEGIN */
//JQ FILE UPLOAD FUNCTION
var formURL = $(".edit_product").attr("action") + "/product_images";
$('#file-select').fileupload({
url:formURL,
type:'POST',
dataType: 'script',
add: function (e, data) {
data.context = $('<div class="img uploading"/>').appendTo("#product-images");
data.submit();
},
done: function (e, data) {
console.log(data.result);
$.each(data.result.files, function (index, file) {
console.log(file);
});
//$(".no_images").addClass("hidden");
},
option: {
autoUpload: true,
}
});
});
控制器
class ProductImagesController < ApplicationController
before_action :set_product_image, only: [:show, :destroy]
def show
end
def create
@product = Product.find(params[:product_id])
@product_image = @product.product_images.create(product_image_params)
if @product_image.save
respond_to do |format|
format.js
end
else
render :json => [{:error => "custom_failure"}], :status => 304
end
end
def destroy
@product_image.destroy
render :json => true
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product_image
@product_image = ProductImage.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_image_params
params.require(:product_image).permit(:product_id, :file, :title, :caption)
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 routing jquery-file-upload