使用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
Than my form:
= 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 -缺少模板项/continue, application/continue with {:locale=>[:en], :formats=>[:json],: variables =>[],: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。动词或其他适当的JSON部分。您需要浏览下拉区域的文档,以了解需要什么
- JSON分析错误:Java中AJAX、Javascript和Servlet的意外EOF
- 错误405:向Java控制器(Ajax)发送JSON时找不到POST方法
- AJAX语法错误:JSON.parse:意外字符
- 错误:JSON分析错误:使用角度转换时,属性名称必须是字符串文字
- Json响应错误Json.parse:意外的数据结束错误
- json错误json.parse:意外的数据结束错误
- 语法错误:JSON.parse:意外字符
- 语法错误:JSON 分析错误:意外的标识符“对象”
- 语法错误:JSON.parse:意外的数据结束
- 错误:“JSON.parse:JSON 数据第 1 行第 1 列的意外字符”,当包含其他 php 文件时
- 我有一个有效的 JSON,但我不断收到“语法错误 json.parse 第 1 行意外的数据结尾”
- 错误 JSON 响应中出现意外的令牌<
- 错误 JSON.parse 使用 ajax con pusher PHP
- 错误JSON未定义
- 未捕获的语法错误:JSON.parse上出现意外的标记o
- 无法识别字段-未标记为可忽略错误-JSON-Java对象
- 解析JSON通过javascript得到错误:JSON.解析:意想不到的人物
- 语法错误:JSON 中位置 1 处出现意外的标记 o
- Sencha触摸-错误- JSON恢复
- Sharepoint Check In Rest API错误'JSON流格式不佳'