使用format.js创建动作中的ActionController :: UnknownFormat(ActionController :: UnknownFormat) [英] ActionController::UnknownFormat (ActionController::UnknownFormat) in create action with format.js

查看:130
本文介绍了使用format.js创建动作中的ActionController :: UnknownFormat(ActionController :: UnknownFormat)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,该表单创建带有 dropzone 的图像:

I have a form, that creates images with a 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)

ProductImage is created, but I receive an error: ActionController::UnknownFormat (ActionController::UnknownFormat)

这是我的 create.js.erb 文件:

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

上述给定错误的原因是什么?谢谢。

What is the reason for the given error above? Thanks.

编辑服务器日志:

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'


推荐答案

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

The request format from dropzone is json so you need to add:

format.json {head:ok}

到您的 respond_to 区块。

这篇关于使用format.js创建动作中的ActionController :: UnknownFormat(ActionController :: UnknownFormat)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆