【问题标题】:Uploading with json causes missing template error使用 json 上传会导致缺少模板错误
【发布时间】:2015-10-02 18:44:59
【问题描述】:

我认为dropzone.js 在我提交表单后导致我缺少模板错误。

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

【问题讨论】:

    标签: javascript jquery ruby-on-rails ruby


    【解决方案1】:

    如果你查看错误:

    ActionView::MissingTemplate - 缺少模板项/继续,应用程序/继续使用 {:locale=>[:en], :formats=>[:json], :variants=>[] , :handlers=>[:erb, :builder, :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 部分。您将需要浏览 drop zone 的文档以了解需要什么

    【讨论】:

    • 所以我必须创建一个new.js.erb 或create.js.erb 文件来处理这个问题? continue.js.erb 怎么样?通过 json 渲染页面完全没问题,我只是想让它正常工作。
    • 删除渲染 :new 行并找出您的 @item 无法保存的原因。查看我编辑后的回复
    • 我完全修改了我的问题。感谢您迄今为止的帮助。
    • 我的编辑确认您需要生成对放置区的 JSON 响应。所以它似乎已经解决了你原来的要求。我认为您的新问题是关于如何创建放置区 JSON,文档应该可以帮助您。并发送与您原来的问题不同。如果考虑查看文档,然后开始一个新帖子,其中包含可能出现的未解决问题。
    • 我的编辑确认您需要生成对放置区的 JSON 响应。所以它似乎已经解决了你原来的要求。我认为您的新问题是关于如何创建放置区 JSON,文档应该可以帮助您。并发送与您原来的问题不同。如果考虑查看文档,然后开始一个新的帖子,其中可能会出现未解决的问题。
    猜你喜欢
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多