ฉันใช้ 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
นอกเหนือจากตัวเลือกต่างๆ การคลิกปุ่มส่งบนแบบฟอร์มของฉัน หากมีรูปภาพอยู่ ระบบจะประมวลผลรูปภาพ และหากไม่มีรูปภาพก็จะส่งแบบฟอร์มต่อไป
ถัดไปคือคอนโทรลเลอร์ของฉัน (รหัสที่เกี่ยวข้อง):
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
กว่าแบบฟอร์มของฉัน:
= 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'
ตอนนี้การตั้งค่านี้ทำให้ทุกอย่างทำงานได้แล้ว ก่อนที่ฉันจะได้รับข้อผิดพลาด 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)
ตอนนี้ฉันสงสัยว่าเมื่อใช้ JSON หรือในสถานการณ์นี้ทั้งหมด ฉันจะเปลี่ยนเส้นทางไปยังหน้าแสดงรายการได้อย่างไร หากยังคงบังคับให้ฉันใช้ JSON
อัปเดต
เมื่อเปลี่ยนคำตอบของฉันเมื่อรายการถูกบันทึก:
if @item.save
format.html { redirect_to @item }
format.json { redirect_to @item }
else
มันทำสิ่งนี้:
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)
ดังนั้นฉันเชื่อว่าฉันต้องทำ GET
คำขอเป็น HTML อย่างไรเพื่อให้มันถูกต้อง
อัปเดต 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.....
มันยังคงไม่เปลี่ยนเส้นทางให้ฉันให้ผลลัพธ์เหมือนเดิม
อัปเดต 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 )}
ฉันคิดว่าฉันต้องเปลี่ยนเส้นทางด้วย window.location
ซึ่งเป็นส่วนหนึ่งของ JavaScript ซึ่งอยู่ที่ไหนสักแห่งในโค้ดของฉัน แต่จำเป็นต้องค้นคว้าเพิ่มเติม ฉันไม่สนใจว่าฉันจะสามารถทำงานได้ในรูปแบบ js
หรือ html
ตราบใดที่มันใช้งานได้
อัปเดต 4
โอเค ฉันสามารถรับพฤติกรรมแบบเดียวกันได้ในแอปทดสอบใหม่ล่าสุดที่ GitHub ของฉันที่นี่: https://github.com/justintech/dropzonetest อันนี้ส่ง Invalid JSON response from server.
ดังนั้นฉันเดาว่าแอปจริงของฉันทำสิ่งเดียวกัน แต่มันแค่ผิดพลาดและผ่านการตอบกลับโดยสมบูรณ์ แปลก. รู้สึกอิสระที่จะตรวจสอบออก
JSON
) ของคุณสำเร็จ - person illusionist   schedule 25.10.2015JSON
คุณสามารถลบคำสั่งformat.json {}
ทั้งหมดได้ และตรวจสอบให้แน่ใจว่าคำขอทั้งหมดที่ลูกค้าสามารถทำได้นั้นอยู่ในคำขอHTML
- person illusionist   schedule 26.10.2015else
ทำงานล้มเหลวหรือไม่ - person Timmy Von Heiss   schedule 15.08.2016