【问题标题】:jquery-file-upload POST and nested route - getting no route matches PATCHjquery-file-upload POST 和嵌套路由 - 没有路由匹配 PATCH
【发布时间】: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


    【解决方案1】:

    由于您尝试在产品编辑表单中创建product_image,当JqueryFileUpload 插件向服务器发送数据时,它会发送所有表单数据。

    通过form_for 方法检查生成的HTML 并搜索名为method 的隐藏字段。你会发现值是patch

    <input name="_method" type="hidden" value="patch">
    

    Rails use this param to determine if POST is an PUT, PATCH or DELETE

    你有两个选择来解决这个问题(我更喜欢第一个):

    1. patch 路由添加到您的路由文件路由到product_images#create action:
    2. 在jqueryFileUpload插件的formData回调函数中编辑_method

    【讨论】:

    • 我是不是走错了路?使用 jquery-file-upload 我只想将关联的 ProductImage 添加到 Product,并且在添加 ProductImage 时不需要更新 Product 记录。看起来它发送了我不需要的所有产品表单数据。过去,我在页面中使用单独的表单用于关联记录,并在此表单中隐藏 product_id 字段以进行关联,但我希望将 file_field 保留在此表单的父表单中。跨度>
    • 不,jquery-file-upload 默认是发送所有表单数据。如果要编辑发送到服务器的数据,请使用formData 函数。
    • 我知道我可以使用 formData 覆盖...但是我应该使用 product_images 独有的单独表单发布,而不是使用嵌套产品表单来创建新图像吗?我在过去的项目中就是这样做的
    • 是的,如果您可以为图像使用专有形式,那就更好了。
    • 有时,由于屏幕布局的原因,我无法使用独占形式。在这些情况下,我使用了上面提到的第一个解决方案
    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多