使用json上载会导致模板错误丢失 [英] Uploading with json causes missing template error

查看:91
本文介绍了使用json上载会导致模板错误丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为 dropzone.js 导致我在提交表单后丢失模板错误.

I think dropzone.js, is causing me a missing template error after I submit my form.

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

In my coffeescript js file I have the setup for 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

除了我要执行控制器操作外,我希望它执行以下操作:

Than I have my controller action, I want it to do this:

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'

因此,现在此设置使我可以提交没有任何图像的表单,但是当它有图像时,我可以提交表单.它将在缺少模板错误的情况下为我提供此错误:

So now this setup allows me to submit the form without any images but when it has an image and I submit the form. It will give me this error in the missing template error:

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文件吗?但是我不知道这意味着什么或您使用的设置.有人可以帮我吗?

So I'm guessing I have to make a json file? But I don't know what that means or the setup you use. Can someone help me?

推荐答案

如果您看到错误:

ActionView :: MissingTemplate-缺少模板项/继续,应用程序/继续(带有{:locale => [:en],:formats => [:json] ,:variants => [] ,:handlers => [:erb,:builder,:raw,:ruby,:coffee,:arb,:haml,:jbuilder]}.

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

突出显示的部分是赠品.所请求的模板应该可以处理JSON,但是该操作没有JSON模板的模板(并且您还没有以其他任何方式呈现).

The highlighted parts are the giveaway. The requested template is supposed to handle JSON, but the action doesn't have a template for a JSON template (and you haven't rendered any other way).

这可能是因为您拥有

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

render:渲染json之前的new.没有新模板,并且您错误地尝试渲染两次.丢失render :new行.

render :new before rendering the json. There is no new template and you are incorrectly trying to render twice. Lose the render :new line.

修改

有两个问题.首先,在创建操作@item.save中失败,导致出现render :new行.由于在这种情况下,我假设您确实要返回呈现的JSON,而不是标准用户表单,因此应删除render :new行.这是多余的.

There are two issues. First, in the create action @item.save is failing, leading to the render :new line. Since in this case I'm assuming that you really wanted to return the rendered JSON, not a standard user form, you should remove the render :new line. This is redundant.

第二,您需要通过查看development.log来弄清为什么@item无法正确保存的原因.如果仅出于调试目的将其更新为@item.save!,则可能会为您提供一些其他信息来跟踪故障.

Secondly, you need to work out why your @item is not saving correctly by looking at your development.log. If you update it to @item.save! just for debugging purposes, this may provide you with some additional information with which to trace the fault.

修改2

根据您更新的问题,您的@item现在正在保存.您正在请求 show 部分,尽管似乎未正确定义它或没有适当的items/show.json.erb或其他适当的JSON部分.您将需要浏览放置区的文档,以了解需要什么

Based on your updated question, your @item is now saving. You are requesting the show partial, although it appears that this is not defined correctly or does not have an appropriate items/show.json.erb or other appropriate JSON partial. You will need to explore the documentation for drop zone to know what is needed

这篇关于使用json上载会导致模板错误丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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