代码之家  ›  专栏  ›  技术社区  ›  Alex Zakruzhetskyi

创建操作中的ActionController::UnknownFormat(ActionController::UnknownFormat)格式.js

  •  0
  • Alex Zakruzhetskyi  · 技术社区  · 7 年前

    我有一个窗体,它用 dropzone :

    <%= form_tag "/admin/products/#{@product.slug}/product_images",
                       method: :post,
                       class: 'dropzone needsclick dz-clickable',
                       id: 'form_dropzone',
                       remote: true do %>
            <input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>" />
            <div class="fallback">
              <%= file_field_tag 'media', multiple: true %>
            </div>
          <% end %>
    

     def create
        @product = @current_domain.products.find(params[:product_id])
        @product_image = ProductImage.create(file_name: params[:file],
                                             product_id: @product.id)
    
        respond_to do |format|
          format.js { render 'create.js', content_type: 'text/javascript', layout: 'admin' }
        end
      end
    

    ProductImage 已创建,但我收到一个错误: ActionController::UnknownFormat (ActionController::UnknownFormat)

    这是我的 create.js.erb

    alert('Done'); // This is for test
    

    上面给出的错误的原因是什么?谢谢。

    Started POST "/admin/products/a-test-t-shirt/product_images" for 127.0.0.1 at 2018-07-25 20:23:25 +0300
    Processing by Admin::ProductImagesController#create as JSON
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"FaIiJclyTvEZaYIx157PDGCpSnVO4Xg7n59CLSCi/IfxME84I/EML0Ej5FGmVRfIlNsmaXzJ5prurCkPVN3Zyg==", "file"=>#<ActionDispatch::Http::UploadedFile:0x00007fe607430950 @tempfile=#<Tempfile:/tmp/RackMultipart20180725-7417-19ua3ie.png>, @original_filename="Снимок экрана от 2018-03-20 21-15-02.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"\xD0\xA1\xD0\xBD\xD0\xB8\xD0\xBC\xD0\xBE\xD0\xBA \xD1\x8D\xD0\xBA\xD1\x80\xD0\xB0\xD0\xBD\xD0\xB0 \xD0\xBE\xD1\x82 2018-03-20 21-15-02.png\"\r\nContent-Type: image/png\r\n">, "product_id"=>"a-test-t-shirt"}
      Domain Load (11.1ms)  SELECT  "domains".* FROM "domains" WHERE "domains"."name" = $1 LIMIT $2  [["name", "localhost:3000"], ["LIMIT", 1]]
      ↳ app/controllers/application_controller.rb:26
      Domain Load (0.9ms)  SELECT  "domains".* FROM "domains" WHERE "domains"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
      ↳ app/controllers/admin/admin_controller.rb:29
      User Load (3.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
      ↳ app/controllers/admin/admin_controller.rb:17
      Product Load (1.4ms)  SELECT  "products".* FROM "products" WHERE "products"."domain_id" = $1 AND "products"."slug" = $2 LIMIT $3  [["domain_id", 1], ["slug", "a-test-t-shirt"], ["LIMIT", 1]]
      ↳ app/controllers/admin/product_images_controller.rb:9
       (0.3ms)  BEGIN
      ↳ app/controllers/admin/product_images_controller.rb:10
      Product Load (0.6ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
      ↳ app/controllers/admin/product_images_controller.rb:10
      ProductImage Create (4.6ms)  INSERT INTO "product_images" ("file_name", "product_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["file_name", "Снимок_экрана_от_2018-03-20_21-15-02.png"], ["product_id", 1], ["created_at", "2018-07-25 17:23:25.714816"], ["updated_at", "2018-07-25 17:23:25.714816"]]
      ↳ app/controllers/admin/product_images_controller.rb:10
       (15.2ms)  COMMIT
      ↳ app/controllers/admin/product_images_controller.rb:10
    Completed 406 Not Acceptable in 391ms (ActiveRecord: 37.7ms)
    
    
    
    ActionController::UnknownFormat (ActionController::UnknownFormat):
    
    app/controllers/admin/product_images_controller.rb:13:in `create'
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   larz    5 年前

    请求格式来自 dropzone json

    format.json { head :ok }

    给你的 respond_to 阻止。

        2
  •  0
  •   Demon    7 年前

    您正在使用 respond_to 但您没有指定格式。 ActionController::UnknownFormat