簡體   English   中英

使用json上傳會導致模板錯誤丟失

[英]Uploading with json causes missing template error

我認為dropzone.js導致我提交表單后丟失了模板錯誤。

在我的coffeescript js文件中,我設置了dropzone:

Dropzone.autoDiscover = false

dropzone = new Dropzone('#item-form',
  maxFiles: 1
  maxFilesize: 1
  paramName: 'item[image]'
  headers: "X-CSRF-Token" : $('meta[name="csrf-token"]').attr('content')
  addRemoveLinks: true
  clickable: '#image-preview'
  previewsContainer: '#image-preview'
  thumbnailWidth: 200
  thumbnailHeight: 200
  parallelUploads: 100;
  autoProcessQueue: false
  uploadMultiple: false)

$('#item-submit').click (e) ->
  e.preventDefault()
  e.stopPropagation()
  if dropzone.getQueuedFiles().length > 0
    dropzone.processQueue()
  else
    $('#item-form').submit()
  return
return

dropzone.on 'success', (file, responseText) ->
  window.location.href = '/items/' + responseText.id
  return

比起我的控制器動作,我希望它能夠做到這一點:

def create
    @item = current_user.items.build(item_params)

    respond_to do |format|
      if @item.save
        format.html { redirect_to @item }
        format.json { render :show, status: :created, location: @item }
      else
        format.html { render :new }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end
  end

比我的形式:

= form_for @item, validate: true, html: {id: 'item-form', class: 'form', multipart: true} do |f|
 = f.text_field :name, class: 'form-control'

    %main#image-preview
     Add a Photo
     .fallback
      = f.file_field :image, multiple: false

= f.submit 'Done', id: 'item-submit'

因此,現在此設置允許我提交沒有任何圖像的表單,但是當它有圖像時,我將提交表單。 在缺少的模板錯誤中,它將給我這個錯誤:

Started POST "/items" for 127.0.0.1 at 2015-10-14 03:35:29 -0700
Processing by ItemsController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"lZu8pjXdBY6wso5l1/B1lwgYDMkVmtRjCaPNBeK3uWy0mQItbv0bDaEZfujeEa2jt//S3qJ0f0fiWkIGC7uNFg==", "item"=>{"name"=>"dsdsds"es", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f4779ccd748 @tempfile=#<Tempfile:/tmp/RackMultipart20151014-2664-16b2m09.jpg>, @original_filename="testimg.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[image]\"; filename=\"testimg.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "null"=>"", "commit"=>"Done"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]] 

   (46.1ms)  COMMIT
  Item Store (158.4ms)  {"id":29}
Completed 406 Not Acceptable in 2115ms (Searchkick: 158.4ms | ActiveRecord: 47.7ms)

ActionView::MissingTemplate - Missing template items/show, application/show with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :arb, :haml, :jbuilder]}. Searched in:
...............

所以我猜我必須制作一個json文件嗎? 但是我不知道這意味着什么或您使用的設置。 有人能幫我嗎?

如果您查看錯誤:

ActionView :: MissingTemplate-缺少模板項/繼續,應用程序/繼續缺少{:locale => [:en],: formats => [:json],: variants => [],:handlers => [:erb,:生成器,:raw,:ruby,:coffee,:arb,:haml,:jbuilder]}。

突出顯示的部分是贈品。 所請求的模板應該可以處理JSON,但是該操作沒有JSON模板的模板(並且您沒有以其他任何方式呈現)。

這可能是因為您有

 if @item.save
   redirect_to post_continue_item_path(@item)
 else
   render :new 
   render json: @item.errors, status: :unprocessable_entity**
 end

render:new在渲染json之前。 沒有新模板,並且您錯誤地嘗試渲染兩次。 失去render :new行。

編輯

有兩個問題。 首先,在創建動作@item.save失敗,導致render :new行。 由於在這種情況下,我假設您確實要返回渲染的JSON,而不是標准的用戶表單,因此應刪除render :new行。 這是多余的。

其次,您需要通過查看您的development.log找出為什么@item無法正確保存的原因。 如果將其更新為@item.save! 僅出於調試目的,這可能會為您提供一些其他信息來跟蹤故障。

編輯2

根據您更新的問題,您的@item現在正在保存。 您正在請求show局部,盡管它似乎未正確定義或沒有適當的items / show.json.erb或其他適當的JSON局部。 您將需要瀏覽放置區的文檔以了解需要什么

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM