Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

ruby on rails - ActionController::UnknownFormat (ActionController::UnknownFormat) in create action with format.js

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 %>

and a correspondent controller action:

 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 is created, but I receive an error: ActionController::UnknownFormat (ActionController::UnknownFormat)

Here is my create.js.erb file:

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

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

EDIT Server logs:

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="xD0xA1xD0xBDxD0xB8xD0xBCxD0xBExD0xBA xD1x8DxD0xBAxD1x80xD0xB0xD0xBDxD0xB0 xD0xBExD1x82 2018-03-20 21-15-02.png"
Content-Type: image/png
">, "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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

format.json { head :ok }

to your respond_to block.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...