使用format.js创建动作的ActionController :: UnknownFormat(ActionController :: UnknownFormat)

时间:2018-07-25 16:59:57

标签: ruby-on-rails ruby dropzone.js

我有一个表单,该表单使用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'

2 个答案:

答案 0 :(得分:1)

来自dropzone的请求格式为json,因此您需要添加:

format.json { head :ok }

到您的respond_to区块。

答案 1 :(得分:0)

您正在使用respond_to,但未指定格式。 ActionController::UnknownFormat