Joh*_*ton 13 ruby json ruby-on-rails ruby-on-rails-4
我正在使用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
Run Code Online (Sandbox Code Playgroud)
除了选项之外,如果有图像存在,单击表单上的提交按钮将处理图像,如果没有图像,则无论如何都会提交表单.
接下来是我的控制器(相关代码):
def create
@item = current_user.items.build(item_params)
respond_to do |format|
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
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
Run Code Online (Sandbox Code Playgroud)
比我的形式:
= form_for @item, 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'
Run Code Online (Sandbox Code Playgroud)
所以现在这个设置一切正常.在我收到Missing Template错误之前,因为我需要创建一个show.json.erb模板,在我这样做之后,它在日志中给了我这个:
Started POST "/items" for 127.0.0.1 at 2015-10-16 21:36:18 -0700
Processing by ItemsController#create as JSON
...................
(10.7ms) COMMIT
Item Store (142.4ms) {"id":12}
Rendered items/show.json.erb (0.4ms)
Completed 201 Created in 2759ms (Views: 6.1ms | Searchkick: 142.4ms | ActiveRecord: 11.9ms)
Run Code Online (Sandbox Code Playgroud)
所以现在我想知道何时使用JSON或只是在这个场景中,如果它一直强制我使用JSON,如何重定向到项目显示页面?
UPDATE
保存项目时更改我的响应时:
if @item.save
format.html { redirect_to @item }
format.json { redirect_to @item }
else
Run Code Online (Sandbox Code Playgroud)
它这样做:
Started GET "/items/13" for 127.0.0.1 at 2015-10-16 22:54:29 -0700
Processing by ItemsController#show as JSON
Parameters: {"id"=>"13"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Item Load (0.4ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", 13]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
Rendered items/show.json.erb (0.7ms)
Completed 200 OK in 137ms (Views: 7.5ms | ActiveRecord: 25.2ms)
Run Code Online (Sandbox Code Playgroud)
所以我相信一些我如何做GETHTML请求才能做到正确?
更新2
我在下面做了以下内容,并且还必须创建一个create.json.erb文件.
if @item.save
format.html { redirect_to @item }
format.json { redirect_to item_path(@item, format: :html) } and return
else
Started POST "/items" for 127.0.0.1 at 2015-10-16 23:36:13 -0700
Processing by ItemsController#create as JSON............
(12.9ms) COMMIT
Item Store (426.1ms) {"id":18}
Rendered items/create.json.erb (19.6ms)
Completed 200 OK in 3892ms.....
Run Code Online (Sandbox Code Playgroud)
它仍然没有重定向我给我与以前相同的结果.
更新3
这样做format.json { redirect_to item_path(@item) and return}不会重定向到显示页面,而是重定向到JSON中的进程.和做format.json { redirect_to item_path(@item, format: :html) and return}给我留下了Started GET "/items/19.html这是错误的,因为你不能让一个页面.html结尾.
更新3
仍然没有运气与任何变化:
format.json {render :show, status: :created, location: item_url(@item, format: :html )}
Run Code Online (Sandbox Code Playgroud)
我想我必须重新定位window.locationJavaScript的一部分,在我的代码中的某个地方,但需要做更多的研究.只要它有效,我不在乎我是否可以使用它js或html格式化.
更新4
好的,所以我能够在我的GitHub上的全新测试应用程序中获得相同的行为:https://github.com/justintech/dropzonetest.这个抛出了Invalid JSON response from server.所以我猜我真正的应用程序做同样的事情,但它只是故障和传递作为一个完整的响应.奇怪的.随意检查出来.
如果您希望两种格式都响应相同,请忘记格式部分.您只需要重定向即可.试试吧redirect_to(@item).
编辑:
我查看了你的代码.我发现以下工作:
items_controller.rb:
def create
@item = Item.new(item_params)
respond_to do |format|
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { render json: @item }
else
format.html { render :new }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
在items.coffee中:
$(document).ready ->
# disable auto discover
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()
dropzone.on 'success', (file, responseText) ->
window.location.href = '/items/' + responseText.id
Run Code Online (Sandbox Code Playgroud)
如果你想要你为你的格式做redirect_to的items show页面html,为什么不只是为json格式做同样的事情?:
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { redirect_to @item, notice: 'Item was successfully created.' }
# rest of your codes
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6944 次 |
| 最近记录: |